/* public/t/styles.css — Udder Tracker mobile PWA.
 *
 * Standalone rather than an extension of /admin.css: this file is cached by the
 * service worker as part of the app shell, and admin.css carries a desktop
 * dashboard's worth of rules the phone never uses. The design tokens below are
 * copied from admin.css so both surfaces stay on the brand palette.
 */

:root,
:root[data-theme="light"] {
  color-scheme: light;
  --color-bg: #F3F4F8;
  --color-surface: #FFFFFF;
  --color-surface-alt: #F3F4F8;
  --color-text: #091330;
  --color-text-muted: rgba(9, 19, 48, 0.62);
  --color-border: rgba(9, 19, 48, 0.12);
  --color-primary: #10396E;
  --color-primary-text: #FFFFFF;
  --color-success: #2F8F6B;
  --color-warning: #D47A2C;
  --color-danger: #C23A45;
  --shadow-sheet: 0 -4px 24px rgba(9, 19, 48, 0.18);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --color-bg: #091330;
  --color-surface: #15203F;
  --color-surface-alt: #1B2747;
  --color-text: #F4F4F4;
  --color-text-muted: rgba(244, 244, 244, 0.66);
  --color-border: rgba(244, 244, 244, 0.14);
  --color-primary: #4D7FC2;
  --color-primary-text: #091330;
  --color-success: #3FAE85;
  --color-warning: #E2924B;
  --color-danger: #E0606C;
  --shadow-sheet: 0 -4px 24px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: "Montserrat", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  /* Stop iOS rubber-banding the whole page behind the fixed layout. */
  overscroll-behavior-y: none;
}

h1, h2, h3 { margin: 0; font-weight: 700; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--color-text-muted); font-size: 0.85rem; }

/* ---------------------------------------------------------------- Splash --- */

.splash {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: #10396E;
  padding: 24px;
}
.splash-logo { width: min(70vw, 300px); height: auto; }
.splash-sub {
  color: #FFFFFF;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  font-size: 0.78rem;
  font-weight: 600;
  opacity: 0.85;
}
.splash-msg { color: rgba(255, 255, 255, 0.75); font-size: 0.85rem; margin-top: 18px; text-align: center; }
.btn-ghost-light {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.5);
  color: #FFFFFF;
}

/* ------------------------------------------------------------------ Shell -- */

/* The app owns the viewport exactly once: nothing outside .table-scroll may
   scroll, otherwise the sticky table header detaches on iOS. 100dvh (not vh)
   so the browser's collapsing address bar doesn't hide the last row. */
.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
}

.appbar {
  flex: none;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: 5;
}

.appbar-main {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
}
.appbar-mark { width: 30px; height: 30px; border-radius: 7px; flex: none; }
.appbar-titles { flex: 1 1 auto; min-width: 0; }
.appbar-titles h1 { font-size: 1.05rem; line-height: 1.2; }
.appbar-sub { font-size: 0.72rem; color: var(--color-text-muted); }

.iconbtn {
  flex: none;
  position: relative;
  min-width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--color-text);
  font-size: 1.05rem;
  cursor: pointer;
}
.iconbtn:active { background: var(--color-surface-alt); }
.iconbtn .dot {
  position: absolute;
  top: 7px;
  right: 7px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-primary);
}

.searchrow { display: flex; align-items: center; gap: 6px; padding: 0 10px 8px; }
.searchrow input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 10px 12px;
  font: inherit;
  font-size: 16px; /* < 16px makes iOS Safari zoom on focus */
  color: var(--color-text);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 10px;
}

.chips {
  display: flex;
  gap: 6px;
  padding: 0 10px 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  border-radius: 999px;
  border: 1px solid var(--color-border);
  background: var(--color-surface-alt);
  color: var(--color-text);
  cursor: pointer;
}
.chip-on { background: var(--color-primary); border-color: var(--color-primary); color: var(--color-primary-text); }
.chip-x { opacity: 0.75; font-weight: 700; }

/* ------------------------------------------------------------------ Table -- */

/* Both axes scroll here and nowhere else. `position: sticky` resolves against
   this box, so the header row pins to its top edge and the ref column pins to
   its left edge — the two can be active at once because it is a single
   scrollport. Splitting the axes across two elements is what breaks it. */
.table-scroll {
  flex: 1 1 auto;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
}

.items {
  border-collapse: separate; /* `collapse` drops borders on sticky cells */
  border-spacing: 0;
  width: max-content;
  min-width: 100%;
  font-size: 0.8rem;
}

.items th,
.items td {
  text-align: left;
  padding: 9px 10px;
  white-space: nowrap;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
}

.items thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  background: var(--color-surface-alt);
}

/* Frozen ref column: keeps the row identifiable once the user has scrolled the
   details sideways. z-index 3 on the header cell so it wins on both axes. */
.items .col-ref {
  position: sticky;
  left: 0;
  z-index: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.74rem;
  font-weight: 600;
  box-shadow: 1px 0 0 var(--color-border);
}
.items thead .col-ref { z-index: 3; }

.items .col-title { max-width: 62vw; overflow: hidden; text-overflow: ellipsis; }
.items tbody tr:active td { background: var(--color-surface-alt); }
.items tbody tr:active .col-ref { background: var(--color-surface-alt); }

.badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge-danger { background: rgba(194, 58, 69, 0.14); color: var(--color-danger); }
.badge-warning { background: rgba(212, 122, 44, 0.16); color: var(--color-warning); }
.badge-success { background: rgba(47, 143, 107, 0.16); color: var(--color-success); }
.badge-muted { background: var(--color-surface-alt); color: var(--color-text-muted); }

.liststate { padding: 40px 20px; text-align: center; color: var(--color-text-muted); font-size: 0.88rem; }

.fab {
  position: fixed;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 6;
  width: 56px;
  height: 56px;
  border: 0;
  border-radius: 50%;
  background: var(--color-primary);
  color: var(--color-primary-text);
  font-size: 1.9rem;
  line-height: 1;
  box-shadow: 0 4px 14px rgba(9, 19, 48, 0.3);
  cursor: pointer;
}

/* ----------------------------------------------------------------- Sheets -- */

.backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(9, 19, 48, 0.45); }

.sheet {
  position: fixed;
  z-index: 30;
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  box-shadow: var(--shadow-sheet);
}

.sheet-bottom {
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 88dvh;
  border-radius: 18px 18px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
}

/* Full-screen rather than a side drawer: a 760px drawer on a 390px phone is
   just a worse modal. */
.sheet-full {
  inset: 0;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.sheet-grab {
  flex: none;
  width: 38px;
  height: 4px;
  margin: 8px auto 0;
  border-radius: 2px;
  background: var(--color-border);
}

.sheet-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 12px 10px;
  border-bottom: 1px solid var(--color-border);
}
.sheet-head h2 { font-size: 1rem; flex: 1 1 auto; min-width: 0; }
.sheet-head-titles { flex: 1 1 auto; min-width: 0; }
.sheet-head-titles h2 {
  font-size: 1rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sheet-head-titles .mono { font-size: 0.72rem; }

.sheet-body {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 14px 12px 24px;
}

.sheet-foot {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-top: 1px solid var(--color-border);
  min-height: 46px;
}

.section-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--color-text-muted);
  margin: 4px 0 8px;
}

/* ----------------------------------------------------------------- Fields -- */

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.f { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
/* Filter/menu sheets stack bare fields rather than using the .fields grid. */
.sheet-body > .f { margin-bottom: 12px; }
.f > span { font-size: 0.72rem; font-weight: 600; color: var(--color-text-muted); }
.f-wide { grid-column: 1 / -1; }

.f input,
.f select,
.f textarea,
.link-add input,
.link-add select {
  width: 100%;
  padding: 10px;
  font: inherit;
  font-size: 16px; /* keeps iOS from zooming the viewport on focus */
  color: var(--color-text);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 9px;
}
.f textarea { resize: vertical; min-height: 78px; }
.f-invalid input { border-color: var(--color-danger); }

.more { margin: 16px 0 8px; border-top: 1px solid var(--color-border); padding-top: 12px; }
.more > summary {
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--color-primary);
  padding: 6px 0;
  list-style: none;
}
.more > summary::-webkit-details-marker { display: none; }
.more > summary::before { content: '▸ '; }
.more[open] > summary::before { content: '▾ '; }
.more > .fields { margin-top: 10px; }

/* --------------------------------------------------------------- Templates - */

.template-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 16px; }

.template {
  text-align: left;
  padding: 10px;
  border-radius: 11px;
  border: 1px solid var(--color-border);
  background: var(--color-surface-alt);
  color: var(--color-text);
  cursor: pointer;
  font: inherit;
}
.template strong { display: block; font-size: 0.8rem; font-weight: 700; }
.template span { display: block; font-size: 0.68rem; color: var(--color-text-muted); margin-top: 2px; }
.template-on {
  border-color: var(--color-primary);
  background: var(--color-primary);
  color: var(--color-primary-text);
}
.template-on span { color: var(--color-primary-text); opacity: 0.85; }

/* ------------------------------------------------------------------ Links -- */

.links { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.link {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 10px;
  border: 1px solid var(--color-border);
  border-radius: 9px;
  font-size: 0.78rem;
}
.link-rel { font-size: 0.68rem; color: var(--color-text-muted); white-space: nowrap; }
.link-title { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-add { display: grid; grid-template-columns: 1fr 88px auto; gap: 6px; align-items: center; }

/* ------------------------------------------------------------------- Misc -- */

.btn {
  padding: 11px 16px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
}
.btn-sm { padding: 8px 14px; font-size: 0.8rem; }
.btn-primary { background: var(--color-primary); color: var(--color-primary-text); }
.btn-secondary { background: var(--color-surface-alt); color: var(--color-text); border-color: var(--color-border); }
.btn-ghost { background: transparent; color: var(--color-text); border-color: var(--color-border); }
.btn-danger { background: var(--color-danger); color: #FFFFFF; width: 100%; }
.btn:disabled { opacity: 0.55; }

.rowbtn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 14px 12px;
  margin-bottom: 6px;
  font: inherit;
  font-size: 0.9rem;
  color: var(--color-text);
  text-decoration: none;
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  cursor: pointer;
}
.rowbtn-danger { color: var(--color-danger); }
.rowbtn-primary {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-primary-text);
  font-weight: 600;
}

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stat { padding: 10px; text-align: center; background: var(--color-surface-alt); border-radius: 10px; }
.stat-value { font-size: 1.2rem; font-weight: 700; }
.stat-label { font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--color-text-muted); }

.msg { font-size: 0.8rem; }
.msg.ok { color: var(--color-success); }
.msg.err { color: var(--color-danger); }

.editmeta { margin-top: 18px; font-size: 0.72rem; line-height: 1.6; }
.edit-danger { margin-top: 22px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  max-width: 90vw;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  color: #FFFFFF;
  background: rgba(9, 19, 48, 0.92);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}
.toast-err { background: var(--color-danger); }

/* Roomier layout once there is width for it — tablets and desktop testing. */
@media (min-width: 560px) {
  .fields { grid-template-columns: 1fr 1fr 1fr; }
  .template-grid { grid-template-columns: repeat(3, 1fr); }
}
