/* ==========================================================================
   02 · BASE — reset, typography, and the primitive components every
   screen reuses: buttons, fields, cards, tables, chips, badges.
   ========================================================================== */

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  height: auto;
  /* Do NOT set overflow-x:hidden on html/body — iOS WKWebView then
     refuses to scroll vertically (document + nested). Clip on .app. */
  overflow: visible;
}

body {
  margin: 0;
  min-height: 100dvh;
  height: auto;
  overflow: visible;
  -webkit-overflow-scrolling: touch;
  background: var(--th-page);
  color: var(--th-text);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: var(--leading);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 400;
  color: var(--th-label);
  line-height: var(--leading-tight);
  margin: 0 0 var(--s-2);
}
h1 { font-size: var(--text-2xl); }
h2 { font-size: var(--text-lg); }
h3 { font-size: var(--text-md); }

p { margin: 0 0 var(--s-2); }
a { color: var(--th-accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.hidden { display: none !important; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.hint, .legend {
  font-size: var(--text-xs);
  color: var(--th-text-dim);
  margin: 0;
}
.mono { font-family: var(--font-mono); }

::selection { background: var(--th-selected); }

:focus-visible {
  outline: 2px solid var(--th-ring);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ---- scrollbars ---- */
* { scrollbar-width: thin; scrollbar-color: var(--th-line) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb {
  background: var(--th-line); border-radius: var(--r-full);
  border: 3px solid transparent; background-clip: content-box;
}
*::-webkit-scrollbar-thumb:hover { background: var(--th-text-dim); background-clip: content-box; }

/* ==========================================================================
   Cards
   ========================================================================== */
.card {
  background: var(--th-card);
  border: 1px solid var(--th-line);
  border-radius: var(--r-md);
  padding: var(--s-4);
  margin-bottom: var(--s-3);
  box-shadow: var(--e-1);
  color: var(--th-text);
}
.card--flush { padding: 0; overflow: hidden; }
.card--raised { box-shadow: var(--e-3); }

.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: var(--s-3); margin-bottom: var(--s-3);
}
.section-head h2 { margin: 0; }

/* ==========================================================================
   Buttons
   ========================================================================== */
button, .btn {
  font: 500 var(--text-sm)/1.2 var(--font-body);
  display: inline-flex; align-items: center; justify-content: center;
  gap: var(--s-2);
  padding: 0.5rem 0.9rem;
  border-radius: var(--r-sm);
  border: 1px solid var(--th-line);
  background: var(--th-card);
  color: var(--th-text);
  cursor: pointer;
  transition: background var(--dur-1) var(--ease),
              border-color var(--dur-1) var(--ease),
              transform var(--dur-1) var(--ease),
              box-shadow var(--dur-1) var(--ease);
}
button:hover:not(:disabled), .btn:hover:not(:disabled) {
  background: var(--th-hover);
  border-color: var(--th-accent);
}
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled, .btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--th-accent);
  border-color: var(--th-accent);
  color: var(--th-btn-text);
  font-weight: 600;
  box-shadow: var(--e-2);
}
.btn-primary:hover:not(:disabled) {
  background: var(--th-accent-hover);
  border-color: var(--th-accent-hover);
  color: var(--th-btn-text);
}
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--th-hover); border-color: transparent; }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); }
.btn-sm { padding: 0.3rem 0.6rem; font-size: var(--text-xs); }
.btn-icon {
  padding: 0.4rem; width: 2rem; height: 2rem; border-radius: var(--r-sm);
  background: transparent; border-color: transparent; font-size: var(--text-md);
}
.btn-icon:hover:not(:disabled) { background: var(--th-hover); }
.btn-block { width: 100%; }

.btn-loading { position: relative; pointer-events: none; opacity: 0.9; }
.btn-loading .btn-label { opacity: 0; }
.btn-loading::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 1rem; height: 1rem; border-radius: 50%;
  border: 2px solid color-mix(in srgb, currentColor 30%, transparent);
  border-top-color: currentColor; animation: jv-spin 0.7s linear infinite;
}
@keyframes jv-spin { to { transform: rotate(360deg); } }

/* ==========================================================================
   Form fields
   ========================================================================== */
label {
  display: flex; flex-direction: column; gap: var(--s-1);
  font-size: var(--text-xs); color: var(--th-text-dim);
}
.field-label {
  font-size: var(--text-xs); font-weight: 600; letter-spacing: 0.02em;
  color: var(--th-text-dim);
}

input, select, textarea {
  font: 400 var(--text-sm)/1.4 var(--font-body);
  padding: 0.5rem 0.65rem;
  border-radius: var(--r-sm);
  border: 1px solid var(--th-line);
  background: var(--th-input-bg);
  color: var(--th-text);
  width: 100%;
  transition: border-color var(--dur-1) var(--ease), box-shadow var(--dur-1) var(--ease);
}
input:hover:not(:disabled), select:hover:not(:disabled) { border-color: var(--th-text-dim); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--th-accent);
  box-shadow: 0 0 0 3px var(--th-ring);
}
input[type="checkbox"], input[type="radio"] {
  width: auto; accent-color: var(--th-accent);
}
input::placeholder { color: color-mix(in srgb, var(--th-text-dim) 70%, transparent); }
input[readonly] { background: var(--th-page-alt); color: var(--th-text-dim); }

.field { display: flex; flex-direction: column; gap: var(--s-1); }
.field-error {
  font-size: var(--text-xs); color: var(--danger);
  display: flex; align-items: center; gap: var(--s-1);
}
.field.is-invalid input, .field.is-invalid select { border-color: var(--danger); }

.form-grid {
  display: grid; gap: var(--s-3);
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

/* ==========================================================================
   Chips, badges, pills
   ========================================================================== */
.chip {
  display: inline-flex; align-items: center; gap: var(--s-2);
  padding: 0.28rem 0.7rem;
  border-radius: var(--r-full);
  border: 1px solid var(--th-line);
  background: var(--th-card);
  font-size: var(--text-xs);
  color: var(--th-text);
  white-space: nowrap;
}
.chip--accent {
  background: var(--th-accent-soft);
  border-color: color-mix(in srgb, var(--th-accent) 35%, transparent);
  color: var(--th-label);
}
.badge {
  display: inline-flex; align-items: center; gap: var(--s-1);
  font: 500 var(--text-2xs)/1 var(--font-mono);
  letter-spacing: 0.05em; text-transform: uppercase;
  padding: 0.25rem 0.5rem; border-radius: var(--r-full);
  background: var(--th-page-alt); color: var(--th-text-dim);
}
.badge--ok { background: var(--ok-soft); color: var(--ok); }
.badge--warn { background: var(--warn-soft); color: var(--warn); }
.badge--danger { background: var(--danger-soft); color: var(--danger); }
.badge--info { background: var(--info-soft); color: var(--info); }

/* ==========================================================================
   Segmented control
   ========================================================================== */
.segmented {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--th-page-alt);
  border: 1px solid var(--th-line);
  border-radius: var(--r-full);
}
.segmented button {
  border: none; background: transparent; color: var(--th-text-dim);
  padding: 0.28rem 0.8rem; border-radius: var(--r-full);
  font-size: var(--text-xs); font-weight: 600;
}
.segmented button:hover:not(.is-on) { background: var(--th-hover); color: var(--th-text); }
.segmented button.is-on {
  background: var(--th-card); color: var(--th-label);
  box-shadow: var(--e-1);
}

/* ==========================================================================
   Tables
   ========================================================================== */
table { border-collapse: collapse; width: 100%; font-size: var(--text-sm); }
th, td {
  text-align: left; padding: 0.45rem 0.6rem;
  border-bottom: 1px solid var(--th-line-soft);
}
thead th {
  font: 600 var(--text-2xs)/1.3 var(--font-body);
  letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--th-text-dim);
  background: var(--th-page-alt);
  position: sticky; top: 0; z-index: 1;
}
tbody tr:hover { background: var(--th-hover); }
.table-scroll {
  overflow: auto; border: 1px solid var(--th-line);
  border-radius: var(--r-sm); max-height: 70vh;
}
.table-scroll table { border: 0; }

/* Phone/Capacitor: drop the nested vertical max-height so the page
   owns Y-scroll, but keep overflow-x so wide tables can pan sideways.
   overflow-y:hidden with no max-height does not clip — the box grows
   with content; only horizontal overflow creates a scrollport. */
html.is-phone .table-scroll,
html.is-capacitor .table-scroll,
html.chart-paging .table-scroll {
  max-height: none;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
  touch-action: pan-x pan-y;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

/* ==========================================================================
   Empty / loading states
   ========================================================================== */
.empty-actions {
  display: flex; flex-wrap: wrap; gap: 0.5rem;
  justify-content: center; margin-top: 0.25rem;
}
.empty-state {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: var(--s-3);
  padding: var(--s-7) var(--s-4); text-align: center;
  color: var(--th-text-dim);
}
.empty-state-icon { font-size: 2.5rem; opacity: 0.6; line-height: 1; }
.empty-state h3 { margin: 0; color: var(--th-text); }
.empty-state p { max-width: 34ch; margin: 0; font-size: var(--text-sm); }

.skeleton {
  background: linear-gradient(90deg, var(--th-page-alt) 25%,
              var(--th-line-soft) 50%, var(--th-page-alt) 75%);
  background-size: 200% 100%;
  animation: jv-shimmer 1.4s infinite;
  border-radius: var(--r-sm);
}
@keyframes jv-shimmer { to { background-position: -200% 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
