/*
 * Base layer: element defaults and the shared primitives every view composes
 * (buttons, fields, panels, tables, badges, notices, dialogs, tabs).
 * Base element rules sit inside :where() so any component rule can override
 * them without a specificity contest.
 */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { min-width: 320px; background: var(--canvas); accent-color: var(--brand); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--canvas);
  font-family: var(--font-ui);
  font-size: var(--text-md);
  line-height: 1.5;
  font-synthesis: none;
  -webkit-font-smoothing: antialiased;
}
::selection { color: var(--on-brand); background: var(--brand); }

:where(h1, h2, h3, h4) { margin: 0; color: var(--ink); font-weight: 650; line-height: 1.3; letter-spacing: 0; }
:where(p) { margin: 0; }
:where(a) { color: var(--brand); text-underline-offset: 2px; }
:where(a:hover) { color: var(--brand-hover); }
:where(small) { font-size: var(--text-xs); }
:where(button, input, select, textarea) { font: inherit; color: inherit; }
:where(button) { cursor: pointer; }
:where(button:disabled) { cursor: not-allowed; opacity: .55; }
:where(svg) { flex: 0 0 auto; }

:where(button, input, select, textarea, a, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}

/* ---------- Fields ---------- */
:where(label) { min-width: 0; display: grid; gap: 6px; color: var(--ink-2); font-size: var(--text-sm); font-weight: 550; }
/* A span that opens a label is its caption; a span or small that follows the
   control is help text. */
:where(label > span) { color: inherit; font-size: inherit; font-weight: inherit; }
:where(label > :is(input, select, textarea, div) ~ span, label > small, label > span > small) { color: var(--muted); font-size: var(--text-xs); font-weight: 400; line-height: 1.5; }
label .required { display: inline; margin-inline-end: 3px; color: var(--red); }
#confirmPasswordField:not(:has(:required)) .required { display: none; }

:where(input:not([type="checkbox"], [type="radio"], [type="file"], [type="range"]), select, textarea) {
  width: 100%;
  min-width: 0;
  min-height: var(--control-height);
  padding: 7px 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  font-size: var(--text-sm);
  font-weight: 400;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
:where(input[type="number"], input[type="month"], input[type="date"]) { font-variant-numeric: tabular-nums; }
:where(textarea) { min-height: 96px; line-height: 1.6; resize: vertical; }
:where(input, select, textarea)::placeholder { color: var(--faint); }
:where(input:not([type="checkbox"], [type="radio"]), select, textarea):hover { border-color: var(--faint); }
:where(input:not([type="checkbox"], [type="radio"]), select, textarea):focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--focus-ring);
  outline: 0;
}
:where(input[readonly]) { color: var(--muted); background: var(--soft); cursor: default; }
:where(input:disabled, select:disabled, textarea:disabled) { color: var(--faint); background: var(--soft); cursor: not-allowed; }
:where(input[type="checkbox"], input[type="radio"]) { width: 16px; height: 16px; min-height: 0; margin: 0; padding: 0; accent-color: var(--brand); cursor: pointer; }
.app-shell :is(input, select, textarea)[aria-invalid="true"] { border-color: var(--red); box-shadow: 0 0 0 3px color-mix(in srgb, var(--red) 22%, transparent); }

.field-grid { display: grid; gap: 14px 12px; }
.field-grid > label { align-content: start; }
.field-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.form-message { min-height: 18px; color: var(--red); font-size: var(--text-xs); line-height: 1.5; }
.form-message:empty { min-height: 0; }
.form-message.success { color: var(--green); }
.danger-text { color: var(--red) !important; }

/* Switch */
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.toggle-row > span:first-child { color: var(--ink); font-size: var(--text-sm); font-weight: 550; }
.toggle-row strong, .toggle-row small { display: block; }
.toggle-row strong { font-weight: 600; }
.toggle-row small { margin-top: 2px; color: var(--muted); font-size: var(--text-xs); font-weight: 400; line-height: 1.5; }
.toggle-row input { position: absolute; width: 1px; height: 1px; min-height: 0; padding: 0; opacity: 0; pointer-events: none; }
.toggle { position: relative; flex: 0 0 auto; width: 38px; height: 22px; border-radius: var(--radius-pill); background: var(--line-strong); transition: background 140ms ease; }
.toggle::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: var(--shadow-xs); transition: transform 140ms ease; }
.toggle-row input:checked + .toggle { background: var(--brand); }
.toggle-row input:checked + .toggle::after { transform: translateX(16px); }
.toggle-row input:focus-visible + .toggle { outline: 2px solid var(--brand); outline-offset: 3px; }

/* ---------- Buttons ---------- */
.primary-button, .secondary-button {
  min-height: var(--control-height);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: var(--text-sm);
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
}
.primary-button { color: var(--on-brand); border-color: var(--brand); background: var(--brand); }
.primary-button:hover:not(:disabled) { border-color: var(--brand-hover); background: var(--brand-hover); }
.secondary-button { color: var(--ink-2); border-color: var(--line-strong); background: var(--surface); }
.secondary-button:hover:not(:disabled) { color: var(--ink); border-color: var(--faint); background: var(--soft); }
.danger-button { color: var(--red); }
.danger-button:hover:not(:disabled) { color: var(--red); border-color: var(--red-line); background: var(--red-soft); }
.primary-button svg, .secondary-button svg { width: 16px; height: 16px; }

.icon-button {
  width: var(--control-height);
  height: var(--control-height);
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  color: var(--ink-2);
  background: var(--surface);
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
}
.icon-button:hover:not(:disabled) { color: var(--ink); border-color: var(--faint); background: var(--soft); }
.icon-button svg { width: 17px; height: 17px; }
.icon-button.small { width: 28px; height: 28px; }
.icon-button.small svg { width: 14px; height: 14px; }
button.icon-button.action-icon { display: inline-flex; align-items: center; justify-content: center; vertical-align: middle; }
.icon-button.loading svg, .update-button.checking svg, #refreshFbaFees.loading svg { animation: spin 800ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.file-button { position: relative; overflow: hidden; }
.file-button input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.copy-button { padding: 0; border: 0; color: var(--brand); background: transparent; font-size: var(--text-xs); font-weight: 600; }
.copy-button:hover { text-decoration: underline; }

.action-group, .section-actions { display: flex; align-items: center; gap: 8px; }
.module-actions { display: flex; align-items: center; gap: 2px; }
.module-actions button { width: 28px; height: 28px; display: grid; place-items: center; padding: 0; border: 0; border-radius: var(--radius-sm); color: var(--faint); background: transparent; }
.module-actions button:hover { color: var(--ink); background: var(--sunken); }
.module-actions button svg { width: 15px; height: 15px; }

/* ---------- Panels ---------- */
.band, .form-group, .scenario-section, .cost-section {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface);
}
.band { padding: 20px; margin-bottom: 16px; }
.form-band { margin-bottom: 16px; }

.section-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.section-heading h3, .planner-controls h3 { display: flex; align-items: center; gap: 8px; font-size: var(--text-lg); }
.section-heading h3 svg { width: 18px; height: 18px; color: var(--muted); }
.section-heading p, .view-intro p, .planner-controls p, .settings-description { margin-top: 4px; color: var(--muted); font-size: var(--text-xs); line-height: 1.6; }

.form-subheading { margin: 18px 0 10px; padding-bottom: 8px; border-bottom: 1px solid var(--line); color: var(--muted); font-size: var(--text-xs); font-weight: 600; }
.form-subheading:first-of-type { margin-top: 0; }
.form-subheading-row { min-height: 32px; display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.form-subheading-row h4 { color: var(--ink-2); font-size: var(--text-sm); font-weight: 600; }

/* ---------- Figures ---------- */
.kpi-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.kpi-card { min-width: 0; padding: 16px 18px; border: 1px solid var(--line); border-radius: var(--radius-lg); background: var(--surface); }
.kpi-card .kpi-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: var(--muted); font-size: var(--text-xs); font-weight: 550; }
.kpi-card .kpi-icon { width: 28px; height: 28px; display: grid; place-items: center; border-radius: var(--radius-sm); color: var(--muted); background: var(--soft); }
.kpi-card .kpi-icon svg { width: 15px; height: 15px; }
.kpi-card strong { display: block; margin-top: 10px; overflow-wrap: anywhere; color: var(--ink); font-family: var(--font-figure); font-size: var(--text-2xl); line-height: 1.15; font-weight: 600; font-variant-numeric: tabular-nums; }
.kpi-card small { display: block; margin-top: 6px; color: var(--muted); font-size: var(--text-xs); line-height: 1.5; }
.kpi-card > small { overflow-wrap: anywhere; }
.kpi-card.positive strong { color: var(--green); }
.kpi-card.negative strong { color: var(--red); }
.kpi-card.positive .kpi-icon { color: var(--green); background: var(--green-soft); }
.kpi-card.negative .kpi-icon { color: var(--red); background: var(--red-soft); }

.money-stack { display: flex; flex-direction: column; min-width: 0; }
.money-stack > span { overflow-wrap: anywhere; }
.money-stack > small { margin-top: 2px; color: var(--muted); font-family: var(--font-ui); font-size: var(--text-xs); line-height: 1.3; font-weight: 400; }
.money-stack.compact { align-items: flex-end; }

/* ---------- Tables ---------- */
.table-wrap { width: 100%; overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
th { padding: 10px 12px; border-bottom: 1px solid var(--line); color: var(--muted); background: var(--soft); font-size: var(--text-xs); font-weight: 600; text-align: left; white-space: nowrap; }
td { padding: 9px 12px; border-bottom: 1px solid var(--line); color: var(--ink); vertical-align: middle; font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--soft); }
td input, td select { min-width: 88px; min-height: 32px; padding: 5px 8px; }
td .icon-button { width: 30px; height: 30px; border-color: transparent; background: transparent; }
td .icon-button:hover:not(:disabled) { border-color: var(--line-strong); background: var(--surface); }
tfoot td { border-top: 1px solid var(--line-strong); border-bottom: 0; background: var(--soft); font-weight: 650; }

/* ---------- Badges and notices ---------- */
.status-badge { display: inline-flex; align-items: center; padding: 2px 9px; border: 1px solid var(--green-line); border-radius: var(--radius-pill); color: var(--green); background: var(--green-soft); font-size: var(--text-xs); font-weight: 600; white-space: nowrap; }
.status-badge.no { border-color: var(--red-line); color: var(--red); background: var(--red-soft); }
.status-badge.pending { border-color: var(--line); color: var(--muted); background: var(--soft); }
.region-pill { padding: 2px 8px; border-radius: var(--radius-pill); color: var(--brand); background: var(--brand-soft); font-size: var(--text-xs); font-weight: 600; white-space: nowrap; }
.analysis-month-badge { display: inline-flex; align-items: center; min-height: 24px; padding: 2px 10px; border-radius: var(--radius-pill); color: var(--amber-strong); background: var(--amber-soft); font-size: var(--text-xs); font-weight: 600; white-space: nowrap; }
.status-dot { width: 8px; height: 8px; flex: 0 0 auto; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 3px var(--green-soft); }

.calculation-warning { display: flex; align-items: flex-start; gap: 10px; margin: 0 0 14px; padding: 11px 14px; border: 1px solid var(--amber-line); border-radius: var(--radius); color: var(--amber-strong); background: var(--amber-soft); font-size: var(--text-sm); line-height: 1.6; }
.calculation-warning svg { width: 17px; height: 17px; margin-top: 2px; }
.omitted-cost-warning { border-color: var(--red-line); color: var(--red); background: var(--red-soft); }
.omitted-cost-warning strong { font-weight: 650; }
.dimensional-warning { border-color: var(--brand-line); color: var(--brand-dark); background: var(--brand-soft); }
.fee-rule-note { margin: 8px 0 12px; padding: 8px 11px; border-left: 2px solid var(--amber); border-radius: 0 var(--radius-sm) var(--radius-sm) 0; color: var(--muted); background: var(--amber-soft); font-size: var(--text-xs); line-height: 1.6; }
.fee-rule-note:last-child { margin-bottom: 0; }

.empty-state { grid-column: 1 / -1; padding: 48px 20px; color: var(--muted); font-size: var(--text-sm); text-align: center; }
.empty-state svg { width: 28px; height: 28px; margin-bottom: 8px; color: var(--faint); }
.empty-state.wide { grid-column: 1 / -1; }

.sync-conflict { margin: 20px var(--page-gutter) 0; border-color: var(--red-line); background: var(--red-soft); }
.sync-conflict h2 { font-size: var(--text-lg); }
.sync-conflict p { margin: 6px 0 14px; color: var(--ink-2); font-size: var(--text-sm); line-height: 1.7; }
.sync-conflict .action-group { flex-wrap: wrap; }

.toast { position: fixed; left: 50%; top: calc(var(--topbar-height) + 12px + env(safe-area-inset-top, 0px)); z-index: 90; width: max-content; max-width: min(360px, calc(100vw - 32px)); padding: 10px 16px; border-radius: var(--radius); color: var(--surface); background: var(--ink); box-shadow: var(--shadow-md); font-size: var(--text-sm); overflow-wrap: anywhere; opacity: 0; transform: translate(-50%, -8px); pointer-events: none; transition: opacity 160ms ease, transform 160ms ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Tabs ---------- */
.subtabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--line); }
.subtabs button { min-height: 40px; display: inline-flex; align-items: center; gap: 8px; padding: 0 14px; border: 0; border-bottom: 2px solid transparent; color: var(--muted); background: transparent; font-size: var(--text-sm); font-weight: 550; }
.subtabs button svg { width: 16px; height: 16px; }
.subtabs button:hover { color: var(--ink); }
.subtabs button.active { border-bottom-color: var(--brand); color: var(--brand); }
.segmented, .auth-switch { display: inline-flex; padding: 3px; border-radius: var(--radius); background: var(--sunken); }
.auth-switch { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
.segmented button, .auth-switch button { min-height: 32px; padding: 0 12px; border: 0; border-radius: var(--radius-sm); color: var(--muted); background: transparent; font-size: var(--text-sm); font-weight: 600; }
.segmented button.active, .auth-switch button.active { color: var(--ink); background: var(--surface); box-shadow: var(--shadow-xs); }

/* ---------- Dialogs ---------- */
dialog { color: var(--ink); border: 1px solid var(--line); border-radius: var(--radius-lg); background: var(--surface); box-shadow: var(--shadow-lg); }
dialog::backdrop { background: var(--overlay); }
.update-dialog { width: min(520px, calc(100vw - 32px)); padding: 0; }
.update-dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; padding: 20px 22px 16px; border-bottom: 1px solid var(--line); }
.update-dialog-head h2 { font-size: var(--text-xl); }
.update-dialog-status { padding: 16px 22px 4px; color: var(--muted); font-size: var(--text-sm); line-height: 1.6; }
.update-dialog-status.available { color: var(--amber-strong); font-weight: 600; }
.update-dialog-status.current { color: var(--green); }
.update-dialog-status.error { color: var(--red); }
.version-comparison { display: grid; grid-template-columns: minmax(0, 1fr) 24px minmax(0, 1fr); align-items: center; gap: 12px; padding: 14px 22px 20px; }
.version-comparison > div { min-width: 0; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--soft); }
.version-comparison > svg { width: 18px; height: 18px; color: var(--faint); }
.version-comparison span, .version-comparison strong, .version-comparison small { display: block; }
.version-comparison span { color: var(--muted); font-size: var(--text-xs); }
.version-comparison strong { margin-top: 4px; font-family: var(--font-figure); font-size: var(--text-xl); line-height: 1.2; }
.version-comparison small { min-height: 18px; margin-top: 4px; overflow: hidden; color: var(--muted); text-overflow: ellipsis; white-space: nowrap; }
.update-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 22px 20px; }
.forced-dialog { width: min(460px, calc(100vw - 32px)); }

.auth-form { display: grid; gap: 16px; padding: 20px 22px 22px; }
.auth-form .primary-button, .auth-form .secondary-button { width: 100%; min-height: 40px; }
.password-control { position: relative; display: flex; align-items: center; }
.password-control input { width: 100%; padding-right: 64px; }
.password-control button { position: absolute; right: 4px; min-width: 48px; min-height: 28px; border: 0; border-radius: var(--radius-sm); color: var(--brand); background: transparent; font-size: var(--text-xs); font-weight: 600; }
.password-control button:hover { background: var(--brand-soft); }
.password-generate-row { display: grid; grid-template-columns: minmax(0, 1fr) var(--control-height); gap: 8px; }

.account-summary { display: flex; align-items: center; gap: 12px; padding: 4px 22px 18px; border-bottom: 1px solid var(--line); }
.account-summary strong, .account-summary small { display: block; }
.account-summary strong { font-size: var(--text-lg); }
.account-summary small { margin-top: 2px; color: var(--muted); }
.account-avatar { width: 40px; height: 40px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 50%; color: var(--brand); background: var(--brand-soft); }
.account-avatar svg { width: 20px; height: 20px; }
#accountDialog .update-dialog-head { justify-content: flex-end; padding: 12px 14px 0; border: 0; }
.password-form { gap: 8px; border-bottom: 1px solid var(--line); }
.password-form > label:not(:first-child) { margin-top: 8px; }
.account-actions { display: flex; justify-content: flex-end; padding: 14px 22px 20px; }
#saveStatus[hidden], #accountSyncStatus[hidden] { display: none !important; }

/* ---------- Page-hint preference ---------- */
/* Hides only explanatory copy, never values, validation, status or confirmations. */
html[data-page-hints="hide"] :is(
  #profitKpis .kpi-card > small, #logisticsKpis .kpi-card > small,
  .profit-formula, .cost-formula, .metric-label > small,
  .fee-rule-note, #subscriptionNote,
  .storage-estimate-note:not([role="status"]),
  .product-hint, .brand-hint, .settings-description,
  .section-heading > div > p:not([role]):not([aria-live]),
  #notes-view .view-intro > div > p, .planner-controls > div > p,
  #siteThemeHelp, #noteFormatHelp,
  #aiSettingsPanel .ai-help, #aiSettingsPanel label > small,
  #aiAttachHelp, .ai-composer > .ai-actions > small,
  [data-page-hint]
) { display: none !important; }

/* ---------- Preferences ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}

@media (max-width: 640px) {
  .field-grid.three { grid-template-columns: 1fr; }
  .section-heading { align-items: stretch; flex-direction: column; }
  .section-heading .primary-button { align-self: flex-start; }
  .section-actions { align-items: flex-start; flex-direction: column; }
  .version-comparison { grid-template-columns: 1fr; }
  .version-comparison > svg { justify-self: center; transform: rotate(90deg); }
  .update-dialog-actions > button { flex: 1; }
}

.kpi-card small .positive, .dash-head .positive { color: var(--green); font-weight: 600; }
.kpi-card small .negative { color: var(--red); font-weight: 600; }
