/* ==========================================================================
   Components — the reusable visual vocabulary
   ========================================================================== */

/* ---- Card -------------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: var(--card-pad);
  box-shadow: var(--shadow-sm);
  position: relative;
}
.card-quiet {
  background: var(--surface-2);
  box-shadow: none;
}
.card-flush {
  padding: 0;
  overflow: hidden;
}
.card-hd {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}
.card-hd h3,
.card-hd h2 {
  font-size: var(--text-md);
  font-weight: 800;
}
.card-sub {
  font-size: var(--text-sm);
  color: var(--ink-2);
  margin-top: 2px;
}
.card-link {
  display: block;
  width: 100%;
  text-align: left;
  transition: transform var(--dur-1) var(--ease), box-shadow var(--dur-1) var(--ease);
}
.card-link:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  text-decoration: none;
}

/* ---- Button ------------------------------------------------------------ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: 10px 18px;
  min-height: 42px;
  border-radius: var(--r-pill);
  font-size: var(--text-base);
  font-weight: 700;
  border: 1px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  transition: transform var(--dur-1) var(--ease), background var(--dur-1) var(--ease),
    box-shadow var(--dur-1) var(--ease), opacity var(--dur-1) var(--ease);
  user-select: none;
}
.btn:active:not(:disabled) {
  transform: scale(0.97);
}
.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
}
.btn:hover {
  text-decoration: none;
}

.btn-primary {
  background: var(--accent);
  color: var(--on-accent);
  box-shadow: 0 6px 16px var(--accent-ring);
}
.btn-primary:hover:not(:disabled) {
  filter: brightness(1.07);
}

.btn-secondary {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: transparent;
}
.btn-secondary:hover:not(:disabled) {
  filter: brightness(0.97);
}

.btn-ghost {
  background: transparent;
  color: var(--ink-2);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--surface-sunken);
  color: var(--ink);
}

.btn-outline {
  background: var(--surface);
  border-color: var(--line-strong);
  color: var(--ink);
}
.btn-outline:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover:not(:disabled) {
  filter: brightness(1.07);
}

.btn-sm {
  min-height: 34px;
  padding: 6px 13px;
  font-size: var(--text-sm);
}
.btn-lg {
  min-height: 50px;
  padding: 13px 26px;
  font-size: var(--text-md);
}
.btn-block {
  width: 100%;
}
.btn-icon {
  padding: 0;
  width: 42px;
  min-height: 42px;
  border-radius: var(--r-md);
}
.btn-icon.btn-sm {
  width: 34px;
  min-height: 34px;
}

.btn .spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-top-color: transparent;
  animation: spin 0.7s linear infinite;
}

/* ---- Icon button (bare) ------------------------------------------------ */
.iconbtn {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: var(--r-sm);
  color: var(--ink-2);
  transition: background var(--dur-1) var(--ease), color var(--dur-1) var(--ease);
  flex: none;
}
.iconbtn:hover {
  background: var(--surface-sunken);
  color: var(--ink);
}
.iconbtn.is-danger:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

/* ---- Badge / pill ------------------------------------------------------ */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: var(--r-pill);
  font-size: var(--text-xs);
  font-weight: 700;
  background: var(--surface-sunken);
  color: var(--ink-2);
  white-space: nowrap;
  line-height: 1.6;
}
.badge-accent {
  background: var(--accent-soft);
  color: var(--accent);
}
.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);
}
.badge .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}

/* ---- Form fields ------------------------------------------------------- */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field > label,
.field-label {
  font-size: var(--text-sm);
  font-weight: 700;
  color: var(--ink-2);
}
.field-hint {
  font-size: var(--text-xs);
  color: var(--ink-3);
}
.field-error {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--danger);
  display: flex;
  align-items: center;
  gap: 5px;
}

.input,
.select,
.textarea {
  width: 100%;
  padding: 11px 14px;
  min-height: 44px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  transition: border-color var(--dur-1) var(--ease), box-shadow var(--dur-1) var(--ease);
  appearance: none;
}
.textarea {
  min-height: 92px;
  resize: vertical;
  line-height: 1.5;
}
.input::placeholder,
.textarea::placeholder {
  color: var(--ink-3);
}
.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.input[aria-invalid="true"],
.textarea[aria-invalid="true"],
.select[aria-invalid="true"] {
  border-color: var(--danger);
}
.select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238a8aa5' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
  cursor: pointer;
}
.input-group {
  position: relative;
  display: flex;
  align-items: center;
}
.input-group .input {
  padding-right: 46px;
}
.input-group .input-affix {
  position: absolute;
  right: 6px;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: var(--r-xs);
  color: var(--ink-3);
}
.input-group .input-affix:hover {
  color: var(--ink);
  background: var(--surface-sunken);
}
.input-prefix {
  position: absolute;
  left: 14px;
  color: var(--ink-3);
  font-weight: 700;
  pointer-events: none;
}
.input-group.has-prefix .input {
  padding-left: 30px;
}

.fieldset-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-3);
}
@media (max-width: 520px) {
  .fieldset-row {
    grid-template-columns: 1fr;
  }
}

/* Checkbox + switch */
.check {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  font-size: var(--text-sm);
}
.check input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  margin: 1px 0 0;
  flex: none;
  cursor: pointer;
}

.switch {
  position: relative;
  width: 46px;
  height: 27px;
  border-radius: var(--r-pill);
  background: var(--line-strong);
  transition: background var(--dur-2) var(--ease);
  flex: none;
  cursor: pointer;
  border: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 21px;
  height: 21px;
  border-radius: 50%;
  background: #fff;
  box-shadow: var(--shadow-sm);
  transition: transform var(--dur-2) var(--ease);
}
.switch[aria-checked="true"] {
  background: var(--accent);
}
.switch[aria-checked="true"]::after {
  transform: translateX(19px);
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-4);
  padding: var(--sp-3) 0;
  border-bottom: 1px solid var(--line);
}
.switch-row:last-child {
  border-bottom: 0;
}
.switch-row .switch-label {
  font-weight: 600;
}
.switch-row .switch-desc {
  font-size: var(--text-xs);
  color: var(--ink-2);
  margin-top: 2px;
}

/* Segmented control */
.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--surface-sunken);
  border-radius: var(--r-pill);
  max-width: 100%;
  overflow-x: auto;
  scrollbar-width: none;
}
.segmented::-webkit-scrollbar {
  display: none;
}
.segmented button {
  padding: 7px 15px;
  border-radius: var(--r-pill);
  font-size: var(--text-sm);
  font-weight: 700;
  color: var(--ink-2);
  white-space: nowrap;
  transition: background var(--dur-1) var(--ease), color var(--dur-1) var(--ease);
}
.segmented button[aria-selected="true"] {
  background: var(--surface);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}

/* ---- Progress ---------------------------------------------------------- */
.bar {
  height: 9px;
  border-radius: var(--r-pill);
  background: var(--surface-sunken);
  overflow: hidden;
}
.bar > i {
  display: block;
  height: 100%;
  border-radius: var(--r-pill);
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width var(--dur-3) var(--ease-out);
}
.bar.is-ok > i {
  background: var(--ok);
}
.bar.is-warn > i {
  background: var(--warn);
}
.bar.is-danger > i {
  background: var(--danger);
}

.ring {
  display: grid;
  place-items: center;
  position: relative;
  flex: none;
}
.ring svg {
  transform: rotate(-90deg);
  overflow: visible;
}
.ring .ring-track {
  stroke: var(--surface-sunken);
}
.ring .ring-value {
  stroke: var(--accent);
  stroke-linecap: round;
  transition: stroke-dashoffset var(--dur-3) var(--ease-out);
}
.ring .ring-mid {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
  line-height: 1.15;
}
.ring .ring-pct {
  font-weight: 900;
  font-size: var(--text-md);
  font-variant-numeric: tabular-nums;
}
.ring .ring-cap {
  font-size: 10px;
  color: var(--ink-2);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ---- Avatar ------------------------------------------------------------ */
.avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: var(--text-sm);
  color: var(--on-accent);
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  flex: none;
  overflow: hidden;
  text-transform: uppercase;
}
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.avatar-lg {
  width: 72px;
  height: 72px;
  font-size: var(--text-xl);
}

/* ---- Tooltip ----------------------------------------------------------- */
.tip {
  position: relative;
}
.tip::after {
  content: attr(data-tip);
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  transform: translateX(-50%) translateY(4px);
  padding: 6px 10px;
  border-radius: var(--r-xs);
  background: var(--ink);
  color: var(--page);
  font-size: var(--text-xs);
  font-weight: 600;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-1) var(--ease), transform var(--dur-1) var(--ease);
  z-index: 60;
}
.tip:hover::after,
.tip:focus-visible::after {
  opacity: 1;
  transform: translateX(-50%);
}

/* ---- Modal / sheet ----------------------------------------------------- */
.scrim {
  position: fixed;
  inset: 0;
  background: var(--overlay-scrim);
  backdrop-filter: blur(3px);
  z-index: 100;
  display: grid;
  place-items: center;
  padding: var(--sp-4);
  animation: fade-in var(--dur-2) var(--ease) both;
  overflow-y: auto;
}
.modal {
  width: min(560px, 100%);
  max-height: calc(100dvh - 48px);
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--line);
  animation: pop-in var(--dur-2) var(--ease-out) both;
}
.modal-wide {
  width: min(780px, 100%);
}
.modal-hd {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-5) var(--sp-5) 0;
}
.modal-hd h2 {
  font-size: var(--text-lg);
}
.modal-bd {
  padding: var(--sp-4) var(--sp-5);
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}
.modal-ft {
  display: flex;
  gap: var(--sp-3);
  justify-content: flex-end;
  padding: var(--sp-2) var(--sp-5) var(--sp-5);
  flex-wrap: wrap;
}
.modal-ft .grow {
  order: -1;
}

@media (max-width: 640px) {
  .scrim {
    padding: 0;
    align-items: flex-end;
  }
  .modal {
    width: 100%;
    max-height: 92dvh;
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    animation: sheet-up var(--dur-2) var(--ease-out) both;
  }
  .modal-ft {
    padding-bottom: calc(var(--sp-5) + env(safe-area-inset-bottom, 0px));
  }
  .modal-ft .btn {
    flex: 1 1 140px;
  }
}

/* ---- Toast ------------------------------------------------------------- */
#toast-root {
  position: fixed;
  z-index: 300;
  bottom: calc(var(--mobilenav-h) + 16px);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  width: min(420px, calc(100vw - 32px));
  pointer-events: none;
}
@media (min-width: 900px) {
  #toast-root {
    bottom: 24px;
    left: auto;
    right: 24px;
    transform: none;
  }
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 13px 16px;
  border-radius: var(--r-md);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-lg);
  animation: pop-in var(--dur-2) var(--ease-out) both;
  font-size: var(--text-sm);
  font-weight: 600;
}
.toast .toast-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
}
.toast-success .toast-icon {
  background: var(--ok-soft);
  color: var(--ok);
}
.toast-error .toast-icon {
  background: var(--danger-soft);
  color: var(--danger);
}
.toast-info .toast-icon {
  background: var(--info-soft);
  color: var(--info);
}
.toast .toast-action {
  margin-left: auto;
  font-weight: 800;
  color: var(--accent);
  font-size: var(--text-sm);
  flex: none;
}

/* ---- Empty / loading / error states ------------------------------------ */
.empty {
  display: grid;
  justify-items: center;
  text-align: center;
  gap: var(--sp-3);
  padding: var(--sp-8) var(--sp-4);
  color: var(--ink-2);
}
.empty-art {
  width: 72px;
  height: 72px;
  border-radius: var(--r-lg);
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
}
.empty h3 {
  font-size: var(--text-md);
}
.empty p {
  max-width: 34ch;
  font-size: var(--text-sm);
}

.skeleton {
  border-radius: var(--r-sm);
  background: var(--surface-sunken);
  background-image: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.45),
    transparent
  );
  background-size: 320px 100%;
  background-repeat: no-repeat;
  animation: shimmer 1.3s linear infinite;
}
:root[data-dark] .skeleton {
  background-image: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.07),
    transparent
  );
}
.skeleton-line {
  height: 12px;
  margin-bottom: 8px;
}
.skeleton-card {
  height: 120px;
  border-radius: var(--r-lg);
}

.errorbox {
  display: grid;
  justify-items: center;
  text-align: center;
  gap: var(--sp-3);
  padding: var(--sp-7) var(--sp-4);
}
.errorbox .empty-art {
  background: var(--danger-soft);
  color: var(--danger);
}

/* ---- Menu -------------------------------------------------------------- */
.menu {
  position: absolute;
  z-index: 120;
  min-width: 190px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  animation: pop-in var(--dur-1) var(--ease-out) both;
}
.menu button,
.menu a {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 11px;
  border-radius: var(--r-xs);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
  text-align: left;
}
.menu button:hover,
.menu a:hover {
  background: var(--surface-sunken);
  text-decoration: none;
}
.menu button.is-danger {
  color: var(--danger);
}
.menu button.is-danger:hover {
  background: var(--danger-soft);
}
.menu-sep {
  height: 1px;
  background: var(--line);
  margin: 5px 4px;
}
.menu-label {
  padding: 8px 11px 5px;
  font-size: var(--text-xs);
  font-weight: 800;
  color: var(--ink-3);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ---- Chart primitives -------------------------------------------------- */
.chart {
  width: 100%;
}
.chart svg {
  width: 100%;
  overflow: visible;
}
.chart .axis-line {
  stroke: var(--line);
  stroke-width: 1;
}
.chart .grid-line {
  stroke: var(--line);
  stroke-width: 1;
  stroke-dasharray: 2 5;
}
.chart .axis-text {
  fill: var(--ink-3);
  font-size: 10px;
  font-weight: 600;
}
.chart .mark-label {
  fill: var(--ink-2);
  font-size: 10px;
  font-weight: 700;
}
.chart .series-line {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-4);
  margin-top: var(--sp-3);
  font-size: var(--text-xs);
  color: var(--ink-2);
  font-weight: 600;
}
.chart-legend .key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.chart-legend .swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex: none;
}

/* Horizontal labelled bars — the form used for category breakdowns, where
   every bar carries its own visible label and value (this is the relief the
   palette validator asks for on the light surface). */
.hbars {
  display: grid;
  gap: var(--sp-3);
}
.hbar-row {
  display: grid;
  gap: 5px;
}
.hbar-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
  font-size: var(--text-sm);
}
.hbar-name {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 600;
  min-width: 0;
}
.hbar-name .swatch {
  width: 9px;
  height: 9px;
  border-radius: 3px;
  flex: none;
}
.hbar-val {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  flex: none;
}
.hbar-track {
  height: 8px;
  border-radius: var(--r-pill);
  background: var(--surface-sunken);
  overflow: hidden;
}
.hbar-fill {
  height: 100%;
  border-radius: var(--r-pill);
  transition: width var(--dur-3) var(--ease-out);
}

/* Stat tile — a single headline number, no plot. */
.stat {
  display: grid;
  gap: 3px;
}
.stat-value {
  font-size: var(--text-2xl);
  font-weight: 900;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.stat-label {
  font-size: var(--text-xs);
  font-weight: 700;
  color: var(--ink-2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.stat-note {
  font-size: var(--text-xs);
  color: var(--ink-3);
}

/* ---- Confetti ---------------------------------------------------------- */
.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 400;
  overflow: hidden;
}
.confetti i {
  position: absolute;
  top: -14px;
  width: 9px;
  height: 14px;
  border-radius: 2px;
  opacity: 0;
  animation: confetti-fall var(--fall, 2.4s) var(--ease) forwards;
}
@keyframes confetti-fall {
  0% {
    opacity: 1;
    transform: translateY(-10px) rotate(0deg);
  }
  100% {
    opacity: 0;
    transform: translateY(102vh) rotate(680deg);
  }
}
