:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e3e6ee;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --danger: #dc2626;
  --success: #16a34a;
  --shadow: 0 6px 20px rgba(20, 24, 40, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --tabbar-h: 64px;
  --topbar-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1116;
    --surface: #161922;
    --surface-2: #1d212d;
    --border: #262b39;
    --text: #e5e7eb;
    --muted: #9aa1ad;
    --primary: #818cf8;
    --primary-hover: #6366f1;
    --primary-soft: #1e2240;
    --shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

button { font-family: inherit; }

.hidden { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error {
  color: var(--danger);
  font-size: 0.9rem;
  margin: 4px 0 0;
}

/* AUTH */
.auth-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}
.auth-brand {
  text-align: center;
  margin-bottom: 18px;
}
.auth-brand h1 { margin: 8px 0 4px; font-size: 1.5rem; }
.auth-brand .paw { font-size: 2.2rem; }
.tabs {
  display: flex;
  gap: 8px;
  background: var(--surface-2);
  padding: 4px;
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
}
.tab {
  flex: 1;
  background: transparent;
  border: 0;
  padding: 10px;
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
  font-weight: 500;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.auth-form { display: flex; flex-direction: column; gap: 12px; }

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--muted);
}
label > input,
label > select,
label > textarea,
.form input,
.form select,
.form textarea {
  font: inherit;
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }

textarea { resize: vertical; min-height: 60px; font-family: inherit; }

.btn {
  border: 0;
  border-radius: var(--radius-sm);
  padding: 11px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.small { padding: 6px 10px; font-size: 0.85rem; }
.btn.block { width: 100%; }

/* APP */
.app {
  min-height: 100dvh;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 50;
  height: var(--topbar-h);
}
.topbar-inner {
  height: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; flex-shrink: 0; }
.brand .paw { font-size: 1.3rem; }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

.dog-switcher {
  position: relative;
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
}
.dog-switcher-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  max-width: 100%;
  overflow: hidden;
}
.dog-switcher-btn:hover { border-color: var(--primary); }
.dog-switcher-btn .caret { font-size: 0.8rem; color: var(--muted); }
.dog-switcher-btn span:first-child {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}
.dog-switcher-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  min-width: 220px;
  max-width: 80vw;
  z-index: 60;
  overflow: hidden;
}
.dog-switcher-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 10px 14px;
  cursor: pointer;
  color: var(--text);
  font-size: 0.9rem;
}
.dog-switcher-item:hover { background: var(--surface-2); }
.dog-switcher-item.active { color: var(--primary); font-weight: 600; }
.dog-switcher-item .check { margin-left: auto; }
.dog-switcher-divider { height: 1px; background: var(--border); }
.dog-switcher-add {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 10px 14px;
  cursor: pointer;
  color: var(--primary);
  font-size: 0.9rem;
  font-weight: 600;
}
.dog-switcher-add:hover { background: var(--primary-soft); }

/* No-dogs empty state */
.empty-screen {
  min-height: calc(100dvh - var(--topbar-h) - var(--tabbar-h));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.empty-card {
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px 24px;
  max-width: 420px;
  width: 100%;
}
.empty-card .paw.big { font-size: 3rem; margin-bottom: 8px; }
.empty-card h2 { margin: 8px 0 4px; }
.empty-card p { margin: 0 0 18px; }

/* Profile head + member list */
.profile-head,
.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 22px 0 10px;
}
.profile-head { margin-bottom: 12px; }
.section-head .section-title { margin: 0; }

.schedule-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  padding-top: 10px;
}

.entry-meta.future {
  color: var(--primary);
  font-weight: 500;
}
.no-margin { margin: 0 !important; }

.member-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}
.member-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.member-item .username { font-weight: 500; }
.member-item .you { color: var(--muted); font-size: 0.8rem; margin-left: 6px; }
.member-remove {
  background: transparent;
  border: 0;
  color: var(--danger);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 4px 8px;
  border-radius: 6px;
}
.member-remove:hover { background: var(--surface-2); }

.form-inline {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}
.form-inline input {
  flex: 1;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.btn.danger { background: var(--danger); color: white; }
.btn.danger:hover { background: #b91c1c; }
.section-title.danger { color: var(--danger); }

.main {
  max-width: 900px;
  margin: 0 auto;
  padding: 16px;
}
.view { animation: fade 0.18s ease-out; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.view-title { margin: 0 0 14px; font-size: 1.25rem; }
.section-title { margin: 22px 0 10px; font-size: 1rem; color: var(--muted); font-weight: 600; }

/* TAB BAR */
.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 40;
}
.navbtn {
  border: 0;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 0.7rem;
  cursor: pointer;
}
.navbtn .ico { font-size: 1.2rem; }
.navbtn.active { color: var(--primary); }

/* DOG CARD */
.dog-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 14px;
}
.dog-photo-wrap {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  overflow: hidden;
  background: var(--surface-2);
  flex-shrink: 0;
  position: relative;
}
.dog-photo-wrap.large { width: 120px; height: 120px; margin: 0 auto 12px; display: block; }
.dog-photo-wrap img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.photo-placeholder {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem; color: var(--muted);
}
.photo-placeholder.large { font-size: 2.5rem; }
.dog-meta h2 { margin: 0; font-size: 1.15rem; }
.dog-meta p { margin: 2px 0 0; }

/* SUMMARY GRID */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}
.summary-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: left;
}
.summary-tile .label { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.summary-tile .count { font-size: 1.4rem; font-weight: 700; margin-top: 2px; }
.summary-tile .sub { font-size: 0.8rem; color: var(--muted); margin-top: 2px; }

/* LOG GRID */
.log-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
}
.log-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  color: var(--text);
  transition: transform 0.08s, border-color 0.15s;
}
.log-tile:hover { border-color: var(--primary); }
.log-tile:active { transform: scale(0.97); }
.log-tile .big { font-size: 1.8rem; }

/* ENTRY LIST */
.entry-list { display: flex; flex-direction: column; gap: 8px; }
.entry-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  cursor: pointer;
  transition: border-color 0.15s;
}
.entry-item:hover { border-color: var(--primary); }
.entry-icon {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--primary-soft);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}
.entry-body { flex: 1; min-width: 0; }
.entry-title { font-weight: 600; }
.entry-sub { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
.entry-meta { color: var(--muted); font-size: 0.8rem; margin-top: 4px; }
.entry-thumb {
  width: 56px; height: 56px;
  border-radius: 8px; object-fit: cover;
  flex-shrink: 0;
}

.empty {
  text-align: center;
  padding: 24px;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
}

/* CALENDAR */
.cal-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 10px;
}
.cal-controls .icon-btn {
  width: 36px;
  height: 36px;
  font-size: 1.3rem;
  border: 1px solid var(--border);
  background: var(--surface);
}
.cal-controls .icon-btn:hover { border-color: var(--primary); color: var(--primary); }
.cal-month-label {
  font-weight: 600;
  flex: 1;
  text-align: center;
  font-size: 0.95rem;
}
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  margin-bottom: 16px;
}
.cal-header {
  background: var(--surface-2);
  text-align: center;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  padding: 6px 0;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.cal-cell {
  background: var(--surface);
  min-height: 60px;
  padding: 4px 4px 6px;
  border: 0;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  text-align: left;
  font: inherit;
  color: var(--text);
}
.cal-cell:hover { background: var(--surface-2); }
.cal-cell.outside { color: var(--muted); opacity: 0.55; }
.cal-cell.today .cal-day-num {
  background: var(--primary);
  color: white;
  border-radius: 999px;
  width: 22px;
  height: 22px;
  line-height: 22px;
  text-align: center;
}
.cal-cell.selected { background: var(--primary-soft); }
.cal-cell.selected.today { background: var(--primary-soft); }
.cal-day-num {
  font-size: 0.8rem;
  line-height: 1;
  align-self: flex-start;
}
.cal-dots {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  font-size: 0.85rem;
  line-height: 1;
  margin-top: auto;
}
.cal-dot { font-size: 0.75rem; line-height: 1; }
.cal-more {
  font-size: 0.62rem;
  color: var(--muted);
  align-self: center;
  font-weight: 600;
}

@media (min-width: 720px) {
  .cal-cell { min-height: 80px; padding: 6px; }
  .cal-day-num { font-size: 0.9rem; }
  .cal-dot { font-size: 1rem; }
}

/* FILTER ROW */
.filter-row { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.filter-row select { padding: 8px 10px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface); color: var(--text); }

/* FORM */
.form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.form .row { display: flex; gap: 10px; flex-wrap: wrap; }
.form .row > label { flex: 1; min-width: 140px; }

.stars {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 4px 0;
}
.star-btn {
  background: transparent;
  border: 0;
  font-size: 1.6rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  transition: transform 0.08s, color 0.1s;
}
.star-btn:hover { background: var(--surface-2); }
.star-btn:active { transform: scale(0.92); }
.star-btn.active { color: #f59e0b; }

.file-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
  position: relative;
}
.file-button input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

/* MODAL */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  animation: fade 0.15s;
}
.modal-card {
  background: var(--surface);
  width: 100%;
  max-width: 520px;
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  padding: 18px;
  max-height: 90dvh;
  overflow-y: auto;
  animation: slideup 0.2s ease-out;
}
@keyframes slideup { from { transform: translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (min-width: 640px) {
  .modal { align-items: center; padding: 24px; }
  .modal-card { border-radius: var(--radius); max-height: 80dvh; }
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.modal-head h3 { margin: 0; }
.icon-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.2rem;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 50%;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 6px;
}

#entry-photo-preview-wrap img {
  width: 100%;
  max-height: 240px;
  object-fit: cover;
  border-radius: var(--radius-sm);
}

/* TOAST */
.toast {
  position: fixed;
  bottom: calc(var(--tabbar-h) + 16px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--surface);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  z-index: 200;
  box-shadow: var(--shadow);
  animation: toastin 0.18s ease-out;
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* DESKTOP REFINEMENTS */
@media (min-width: 720px) {
  .tabbar {
    position: static;
    height: auto;
    grid-template-columns: repeat(6, max-content);
    gap: 4px;
    padding: 6px;
    border-top: 0;
    border-bottom: 1px solid var(--border);
    justify-content: center;
  }
  .navbtn {
    flex-direction: row;
    gap: 6px;
    padding: 8px 14px;
    font-size: 0.9rem;
    border-radius: var(--radius-sm);
  }
  .navbtn.active { background: var(--primary-soft); }
  .app { padding-bottom: 0; }
  .toast { bottom: 24px; }
}

.section-subtitle {
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  margin: 16px 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.ai-providers {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}
.ai-provider-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.ai-provider-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.ai-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}
.ai-badge.on { background: rgba(22, 163, 74, 0.12); color: var(--success); }
.ai-badge.off { background: var(--surface-2); color: var(--muted); }
.ai-provider-card .form-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.ai-provider-card .form-inline input {
  flex: 1 1 140px;
  min-width: 0;
}
.ai-output {
  margin-top: 10px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.5;
}

/* Barcode scanner */
.barcode-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.barcode-row input {
  flex: 1 1 auto;
  min-width: 0;
}
.barcode-row .btn {
  flex: 0 0 auto;
  white-space: nowrap;
}
.scanner-reader {
  width: 100%;
  min-height: 240px;
  background: #000;
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.verdict-banner {
  margin-top: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 14px;
}
.verdict-banner.verdict-unsafe {
  background: rgba(220, 38, 38, 0.10);
  border-color: rgba(220, 38, 38, 0.45);
  color: #b91c1c;
}
.verdict-banner.verdict-safe {
  background: rgba(22, 163, 74, 0.10);
  border-color: rgba(22, 163, 74, 0.45);
  color: #15803d;
}
.verdict-banner.verdict-unknown {
  background: rgba(234, 179, 8, 0.10);
  border-color: rgba(234, 179, 8, 0.45);
  color: #a16207;
}
.verdict-title {
  font-weight: 600;
}
.verdict-disclaimer {
  margin-top: 4px;
  font-size: 12px;
  opacity: 0.85;
}
.food-check-output {
  margin-top: 12px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.food-check-img {
  max-width: 120px;
  max-height: 120px;
  border-radius: var(--radius-sm);
  display: block;
  margin-bottom: 8px;
}
.food-check-name {
  margin: 4px 0;
  font-size: 16px;
}
