/* app/styles/components.css
   Reusable components. No inline styles are used anywhere: dynamic values go through CSS variables
   set from JavaScript (see hydrate() in core/html.js), so the strict CSP (style-src 'self') holds. */

.icon { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.icon.sm { width: 16px; height: 16px; }
.icon.lg { width: 24px; height: 24px; }
.icon.spin { animation: hf-spin 0.8s linear infinite; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  min-height: var(--control-h); padding: 0 var(--sp-4);
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font-weight: 500; white-space: nowrap; text-decoration: none;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.55; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.btn.primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-3); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { filter: brightness(0.93); background: var(--danger); }
.btn.danger-outline { color: var(--danger-ink); border-color: var(--danger); background: transparent; }
.btn.danger-outline:hover { background: var(--danger-soft); }
.btn.sm { min-height: 32px; padding: 0 var(--sp-3); font-size: var(--fs-sm); }
.btn.icon-only { width: var(--control-h); padding: 0; }
.btn.icon-only.sm { width: 32px; }
.btn[data-busy="true"] { position: relative; pointer-events: none; }
.btn[data-busy="true"] .btn-label { opacity: 0.55; }
@media (max-width: 767px) { .btn { min-height: var(--touch); } .btn.sm { min-height: 36px; } }

/* ---------- Fields ---------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, .field-label { font-size: var(--fs-sm); font-weight: 600; color: var(--text-2); }
.field .req { color: var(--danger); margin-left: 2px; }
.hint { font-size: var(--fs-sm); color: var(--text-3); }
.field-error { font-size: var(--fs-sm); color: var(--danger-ink); display: flex; gap: 6px; align-items: flex-start; }
.field-counter { font-size: var(--fs-xs); color: var(--text-3); text-align: right; font-variant-numeric: tabular-nums; }
.field-counter.over { color: var(--danger-ink); font-weight: 600; }

.input, .select, .textarea {
  width: 100%; min-height: var(--control-h);
  padding: 0 var(--sp-3);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  transition: border-color var(--dur) var(--ease);
}
.textarea { padding: var(--sp-2) var(--sp-3); min-height: 96px; resize: vertical; line-height: 1.55; }
.input::placeholder, .textarea::placeholder { color: var(--text-3); }
.input:hover, .select:hover, .textarea:hover { border-color: var(--text-3); }
.input:focus-visible, .select:focus-visible, .textarea:focus-visible { outline-offset: 0; border-color: var(--focus); }
.input[readonly] { background: var(--surface-3); color: var(--text-2); }
.input:disabled, .select:disabled, .textarea:disabled { background: var(--surface-3); color: var(--text-3); }
[aria-invalid="true"] { border-color: var(--danger) !important; }
.select { appearance: none; padding-right: 36px; background-image: linear-gradient(45deg, transparent 50%, var(--text-3) 50%), linear-gradient(135deg, var(--text-3) 50%, transparent 50%); background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }
@media (max-width: 767px) { .input, .select { min-height: var(--touch); } }

.input-group { position: relative; display: flex; align-items: center; }
.input-group .icon { position: absolute; left: 12px; color: var(--text-3); pointer-events: none; }
.input-group .input { padding-left: 38px; }
.input-group .input-action { position: absolute; right: 4px; }

.check-row { display: flex; align-items: flex-start; gap: var(--sp-3); }
.check-row input[type="checkbox"], .check-row input[type="radio"] { width: 18px; height: 18px; margin: 2px 0 0; accent-color: var(--brand); flex: none; }

.switch { position: relative; display: inline-flex; align-items: center; gap: var(--sp-3); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 44px; height: 24px; margin: 0; cursor: pointer; }
.switch .track { width: 44px; height: 24px; border-radius: var(--radius-full); background: var(--border-strong); position: relative; transition: background var(--dur) var(--ease); flex: none; }
.switch .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform var(--dur) var(--ease); }
.switch input:checked + .track { background: var(--brand); }
.switch input:checked + .track::after { transform: translateX(20px); background: var(--brand-ink); }
.switch input:focus-visible + .track { outline: 2px solid var(--focus); outline-offset: 2px; }

.segmented { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-3); border-radius: var(--radius); border: 1px solid var(--border); max-width: 100%; flex-wrap: wrap; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.segmented span { display: inline-flex; align-items: center; min-height: 32px; padding: 0 var(--sp-3); border-radius: 6px; font-size: var(--fs-sm); color: var(--text-2); white-space: nowrap; }
.segmented input:checked + span { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(16, 24, 40, 0.1); }
.segmented input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 1px; }
@media (max-width: 767px) { .segmented span { min-height: 38px; } }

.form-grid { display: grid; gap: var(--sp-4); grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .span-2 { grid-column: 1 / -1; }
@media (max-width: 767px) { .form-grid { grid-template-columns: minmax(0, 1fr); } }

fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
legend { padding: 0; margin-bottom: 6px; font-size: var(--fs-sm); font-weight: 600; color: var(--text-2); }

/* ---------- Surfaces ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: var(--sp-5); min-width: 0; }
.card.flush { padding: 0; overflow: hidden; }
.card.soft { background: var(--surface-2); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); margin-bottom: var(--sp-4); flex-wrap: wrap; }
.card-head h2, .card-head h3 { display: flex; align-items: center; gap: var(--sp-2); }
.card.danger-zone { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }

.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: var(--sp-4) var(--sp-5); }
.stat-label { color: var(--text-2); font-size: var(--fs-sm); display: flex; align-items: center; gap: var(--sp-2); }
.stat-value { font-size: var(--fs-2xl); font-weight: 600; line-height: 1.25; font-variant-numeric: tabular-nums; }
.stat-note { color: var(--text-3); font-size: var(--fs-xs); }

.banner { display: flex; gap: var(--sp-3); align-items: flex-start; padding: var(--sp-3) var(--sp-4); border-radius: var(--radius); background: var(--info-soft); color: var(--text); border: 1px solid transparent; }
.banner .icon { margin-top: 2px; color: var(--info); }
.banner.warning { background: var(--warning-soft); } .banner.warning .icon { color: var(--warning); }
.banner.danger { background: var(--danger-soft); } .banner.danger .icon { color: var(--danger); }
.banner.success { background: var(--success-soft); } .banner.success .icon { color: var(--success); }
.banner p + p { margin-top: var(--sp-1); }

.badge { display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 9px; border-radius: var(--radius-full); font-size: var(--fs-xs); font-weight: 600; white-space: nowrap; background: var(--neutral-soft); color: var(--text-2); }
.badge .icon { width: 12px; height: 12px; stroke-width: 2.25; }
.badge.success { background: var(--success-soft); color: var(--success); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.danger { background: var(--danger-soft); color: var(--danger-ink); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.pro { background: var(--pro-soft); color: var(--pro); }
.badge.brand { background: var(--accent-soft); color: var(--accent); }

.kbd { display: inline-flex; align-items: center; height: 20px; padding: 0 6px; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 5px; font-family: var(--font-mono); font-size: 0.6875rem; color: var(--text-3); background: var(--surface); }

.progress { height: 8px; border-radius: var(--radius-full); background: var(--surface-3); overflow: hidden; }
.progress > i { display: block; height: 100%; width: var(--w, 0%); background: var(--brand); border-radius: inherit; }
.progress.warn > i { background: var(--orange); }
.progress.danger > i { background: var(--danger); }

.pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--sp-3); font-family: var(--font-mono); font-size: var(--fs-sm); white-space: pre-wrap; word-break: break-word; max-height: 360px; overflow: auto; }

.dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: var(--sp-2) var(--sp-5); }
.dl dt { color: var(--text-3); font-size: var(--fs-sm); }
.dl dd { min-width: 0; word-break: break-word; }

.muted { color: var(--text-3); }
.text-2 { color: var(--text-2); }
.small { font-size: var(--fs-sm); }
.xs { font-size: var(--fs-xs); }
.nums { font-variant-numeric: tabular-nums; }
.trunc { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* ---------- List rows ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-3) 0; border-top: 1px solid var(--border); }
.list > li:first-child { border-top: 0; }
.list .row-link { display: flex; align-items: center; gap: var(--sp-3); width: 100%; padding: 0; border: 0; background: transparent; text-align: left; color: inherit; }
.list .row-link:hover .row-title { text-decoration: underline; }
.row-title { font-weight: 500; }
.row-sub { color: var(--text-3); font-size: var(--fs-sm); }

/* ---------- Toolbar + chips ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: var(--sp-3); align-items: center; margin-bottom: var(--sp-4); }
.toolbar .search { flex: 1 1 260px; max-width: 420px; }
.toolbar .select { width: auto; min-width: 150px; }
.toolbar .count { margin-left: auto; color: var(--text-3); font-size: var(--fs-sm); }

.chip { display: inline-flex; align-items: center; gap: 6px; min-height: 28px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-full); background: var(--surface); font-size: var(--fs-sm); }
.chip button { display: inline-flex; border: 0; background: transparent; padding: 0; color: var(--text-3); }
.chip button:hover { color: var(--danger); }
.chip-list { display: flex; flex-wrap: wrap; gap: var(--sp-2); }

.roll-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; }
.roll-grid label { position: relative; }
.roll-grid input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.roll-grid span { display: flex; align-items: center; justify-content: center; height: 38px; border: 1px solid var(--border-strong); border-radius: var(--radius); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; background: var(--surface); }
.roll-grid input:checked + span { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.roll-grid input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 1px; }

.pick-list { max-height: 260px; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); }
.pick-list label { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-2) var(--sp-3); border-top: 1px solid var(--border); cursor: pointer; }
.pick-list label:first-child { border-top: 0; }
.pick-list label:hover { background: var(--surface-3); }
.pick-list input { accent-color: var(--brand); width: 18px; height: 18px; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: var(--sp-5); border-bottom: 1px solid var(--border); margin-bottom: var(--sp-5); overflow-x: auto; scrollbar-width: none; }
.tabs a, .tabs button {
  position: relative; display: inline-flex; align-items: center; min-height: 44px; padding: 0 2px;
  border: 0; background: transparent; color: var(--text-2); white-space: nowrap; text-decoration: none; font-weight: 500;
}
.tabs a:hover, .tabs button:hover { color: var(--text); text-decoration: none; }
.tabs [aria-selected="true"], .tabs [aria-current="page"] { color: var(--text); font-weight: 600; }
.tabs [aria-selected="true"]::after, .tabs [aria-current="page"]::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: var(--brand); border-radius: 2px; }

/* ---------- Data table ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); }
table.data { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--fs-md); }
table.data th { text-align: left; padding: var(--sp-3) var(--sp-4); font-size: var(--fs-sm); font-weight: 600; color: var(--text-2); background: var(--surface-2); border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th button { display: inline-flex; align-items: center; gap: 4px; border: 0; background: transparent; padding: 0; font: inherit; color: inherit; font-weight: inherit; }
table.data th button:hover { color: var(--text); }
table.data td { padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr[data-row] { cursor: pointer; }
table.data tbody tr[data-row]:hover { background: var(--surface-2); }
table.data tbody tr[data-row]:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
table.data td.actions { text-align: right; white-space: nowrap; }
table.data .cell-main { font-weight: 500; }
table.data .cell-sub { color: var(--text-3); font-size: var(--fs-sm); }

.pagination { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); border-top: 1px solid var(--border); font-size: var(--fs-sm); color: var(--text-2); flex-wrap: wrap; }
.pagination .pages { display: flex; gap: var(--sp-1); align-items: center; }

@media (max-width: 767px) {
  table.data.responsive thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  table.data.responsive, table.data.responsive tbody, table.data.responsive tr, table.data.responsive td { display: block; width: 100%; }
  table.data.responsive tr { padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border); }
  table.data.responsive tbody tr:last-child { border-bottom: 0; }
  table.data.responsive td { display: flex; justify-content: space-between; gap: var(--sp-4); padding: 4px 0; border: 0; text-align: right; }
  table.data.responsive td::before { content: attr(data-label); color: var(--text-3); font-size: var(--fs-sm); text-align: left; flex: none; }
  table.data.responsive td.cell-title { display: block; text-align: left; padding-bottom: var(--sp-2); }
  table.data.responsive td.cell-title::before { content: none; }
  table.data.responsive td.actions { justify-content: flex-end; }
  table.data.responsive td.actions::before { content: none; }
}

/* ---------- Overlays: dialog + drawer + palette ---------- */
.overlay { position: fixed; inset: 0; z-index: 100; background: var(--overlay); display: flex; }
.overlay.center { align-items: center; justify-content: center; padding: var(--sp-4); }
.overlay.right { justify-content: flex-end; }
.overlay.top { align-items: flex-start; justify-content: center; padding: 12vh var(--sp-4) var(--sp-4); }
@keyframes hf-fade { from { opacity: 0; } }
@keyframes hf-pop { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@keyframes hf-slide { from { transform: translateX(32px); opacity: 0; } }
.overlay { animation: hf-fade 120ms var(--ease); }

.dialog { width: min(480px, 100%); max-height: calc(100dvh - 32px); overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-pop); padding: var(--sp-6); animation: hf-pop 160ms var(--ease); }
.dialog.wide { width: min(640px, 100%); }
.dialog h2 { margin-bottom: var(--sp-2); }
.dialog-body { color: var(--text-2); margin-bottom: var(--sp-5); }
.dialog-body > * + * { margin-top: var(--sp-3); }
.dialog-actions { display: flex; justify-content: flex-end; gap: var(--sp-2); flex-wrap: wrap; }
.dialog-icon { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--warning-soft); color: var(--warning); margin-bottom: var(--sp-3); }
.dialog-icon.danger { background: var(--danger-soft); color: var(--danger); }

.drawer { width: min(600px, 100%); height: 100dvh; background: var(--surface); border-left: 1px solid var(--border); display: flex; flex-direction: column; box-shadow: var(--shadow-pop); animation: hf-slide 200ms var(--ease); }
.drawer-head { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--border); }
.drawer-head h2 { flex: 1; min-width: 0; }
.drawer-body { flex: 1; overflow-y: auto; padding: var(--sp-5); }
.drawer-foot { display: flex; justify-content: space-between; gap: var(--sp-2); padding: var(--sp-4) var(--sp-5); border-top: 1px solid var(--border); background: var(--surface); padding-bottom: calc(var(--sp-4) + env(safe-area-inset-bottom)); flex-wrap: wrap; }
.drawer-foot .group { display: flex; gap: var(--sp-2); }
@media (max-width: 767px) { .drawer { width: 100%; } .drawer-body, .drawer-head, .drawer-foot { padding-inline: var(--sp-4); } }

.palette { width: min(620px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-pop); overflow: hidden; animation: hf-pop 160ms var(--ease); }
.palette-input { display: flex; align-items: center; gap: var(--sp-3); padding: 0 var(--sp-4); border-bottom: 1px solid var(--border); }
.palette-input input { flex: 1; min-height: 52px; border: 0; background: transparent; outline: none; font-size: var(--fs-lg); }
.palette-list { max-height: 50dvh; overflow: auto; padding: var(--sp-2); }
.palette-group { padding: var(--sp-2) var(--sp-3) var(--sp-1); font-size: var(--fs-xs); font-weight: 600; color: var(--text-3); }
.palette-item { display: flex; align-items: center; gap: var(--sp-3); width: 100%; min-height: 44px; padding: 0 var(--sp-3); border: 0; border-radius: var(--radius); background: transparent; text-align: left; }
.palette-item[aria-selected="true"] { background: var(--accent-soft); }
.palette-item .sub { color: var(--text-3); font-size: var(--fs-sm); margin-left: auto; }
.palette-empty { padding: var(--sp-6); text-align: center; color: var(--text-3); }

/* ---------- Toasts ---------- */
.toast-root { position: fixed; z-index: 200; right: var(--sp-4); bottom: var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-2); width: min(380px, calc(100vw - 32px)); pointer-events: none; }
.toast { pointer-events: auto; display: flex; gap: var(--sp-3); align-items: flex-start; padding: var(--sp-3) var(--sp-4); background: var(--surface); border: 1px solid var(--border-strong); border-left-width: 4px; border-radius: var(--radius); box-shadow: var(--shadow-pop); animation: hf-pop 180ms var(--ease); }
.toast.success { border-left-color: var(--success); } .toast.success > .icon { color: var(--success); }
.toast.error { border-left-color: var(--danger); } .toast.error > .icon { color: var(--danger); }
.toast.warning { border-left-color: var(--orange); } .toast.warning > .icon { color: var(--warning); }
.toast.info { border-left-color: var(--info); } .toast.info > .icon { color: var(--info); }
.toast-body { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; }
.toast-msg { color: var(--text-2); font-size: var(--fs-sm); word-break: break-word; }
.toast-actions { display: flex; gap: var(--sp-2); margin-top: var(--sp-2); }
@media (max-width: 767px) { .toast-root { right: var(--sp-3); left: var(--sp-3); width: auto; bottom: calc(var(--bottomnav-h) + var(--sp-3) + env(safe-area-inset-bottom)); } }

/* ---------- Loading / empty / error ---------- */
.skeleton { display: block; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface-3) 25%, var(--border) 37%, var(--surface-3) 63%); background-size: 400% 100%; animation: hf-shimmer 1.4s ease infinite; min-height: 14px; }
.skeleton.h-stat { height: 86px; border-radius: var(--radius-lg); }
.skeleton.h-row { height: 52px; }
.skeleton.h-block { height: 180px; border-radius: var(--radius-lg); }
@keyframes hf-shimmer { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }

.state { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--sp-3); padding: var(--sp-10) var(--sp-5); }
.state .state-icon { width: 48px; height: 48px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--accent-soft); color: var(--accent); }
.state.error .state-icon { background: var(--danger-soft); color: var(--danger); }
.state h2 { font-size: var(--fs-lg); }
.state p { color: var(--text-2); max-width: 46ch; }

.fullscreen-state { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: var(--sp-6); background: var(--bg); }
.fullscreen-state .card { max-width: 440px; width: 100%; text-align: center; }
