/* base.css — :root, reset, layout, sidebar, form, chips, modal, language */
:root {
  --bg-primary: #000000;
  --bg-secondary: #111111;
  --bg-tertiary: #1a1a1a;
  --bg-sidebar: #0a0a0a;
  --bg-input: #111111;
  --text-primary: #e8e8e8;
  --text-secondary: #a0a0a0;
  --text-muted: #888888;
  --accent: #d97706;
  --accent-hover: #f59e0b;
  --border: #404040;
  --border-focus: #606060;
  --success: #22c55e;
  --success-bg: rgba(34, 197, 94, 0.1);
  --error: #ef4444;
  --error-bg: rgba(239, 68, 68, 0.1);
  --merge-accent: #7c4dff;

  /* 五行 element colors */
  --el-jin: #f5e6a3;
  --el-mu: #6ee7b7;
  --el-shui: #7dd3fc;
  --el-huo: #fca5a5;
  --el-tu: #c8a266;

  /* Chip styles */
  --chip-bg: #2a2a2a;
  --chip-bd: rgba(255,255,255,.12);
  --chip-ink: #e8e8e8;

  /* Yao bar color */
  --yao: #e8e8e8;

  /* Layout */
  --col-no: 28px;
  --col-yao: 96px;
  --col-right-gap: 8px;

  /* Spacing (4px grid) */
  --sp-1: 0.25rem;   /* 4px */
  --sp-2: 0.5rem;    /* 8px */
  --sp-3: 0.75rem;   /* 12px */
  --sp-4: 1rem;      /* 16px */
  --sp-5: 1.25rem;   /* 20px */
  --sp-6: 1.5rem;    /* 24px */
  --sp-8: 2rem;      /* 32px */

  /* Typography */
  --text-xs: 0.6875rem;  /* 11px — smallest readable Chinese */
  --text-sm: 0.75rem;    /* 12px — chips, auxiliary */
  --text-base: 0.875rem; /* 14px — body text */
  --text-md: 1rem;       /* 16px — headings */
  --text-lg: 1.25rem;    /* 20px — large headings */
  --text-xl: 1.5rem;     /* 24px — page titles */

  /* Radius */
  --radius-xs: 4px;
  --radius-sm: 6px;
  --radius-base: 8px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-full: 999px;
  --radius: 12px;

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.5);

  /* Breakpoints (reference values for documentation; media queries use literals) */
  /* --bp-mobile: 768px;   — mobile ≤768px: hide sidebar, show bottom tabs */
  /* --bp-tablet: 1024px;  — tablet 769-1024px: sidebar rail (icon-only) */
  /* --bp-desktop: 1025px; — desktop >1024px: full sidebar */

  /* Sidebar dimensions */
  --sidebar-width: 200px;
  --sidebar-rail-width: 64px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans SC', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
}

/* Sidebar — 5-item nav, matches mobile tab structure */
.sidebar {
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: sticky;
  top: 0;
}
.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}
.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.sidebar-logo svg { width: 28px; height: 28px; }
.sidebar-logo span { font-size: 1.125rem; font-weight: 600; }
.sidebar-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 1rem 0.75rem;
  overflow-y: auto;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  border-radius: var(--radius-base);
  color: var(--text-secondary);
  font-size: var(--text-base);
  font-weight: 500;
  transition: background-color 0.15s, color 0.15s;
  cursor: pointer;
  border: none;
  background: none;
  font: inherit;
  width: 100%;
  text-align: left;
}
.nav-item:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.nav-item.active { background: rgba(217,119,6,0.12); color: var(--accent); }
.nav-item svg { width: 20px; height: 20px; flex-shrink: 0; }
.sidebar-footer { padding: 0.75rem; border-top: 1px solid var(--border); flex-shrink: 0; }
.nav-user {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem;
  border-radius: var(--radius-base);
}
.user-avatar {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--bg-primary);
  flex-shrink: 0;
}
.user-avatar svg { width: 18px; height: 18px; }
.user-details { flex: 1; min-width: 0; }
.user-name {
  font-size: var(--text-sm);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-credits { font-size: var(--text-xs); color: var(--accent); }

/* Detail header (3-segment: left / center / right) */
.chart-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-primary);
  padding: 0.5rem 0;
}
.header-left, .header-right {
  flex: 0 0 auto;
  min-width: 0;
}
.header-center {
  flex: 1;
  text-align: center;
  font-size: var(--text-base);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 0.5rem;
}
.btn-back-nav {
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.5rem 0;
  font-size: var(--text-sm);
  font-weight: 500;
  font-family: inherit;
  white-space: nowrap;
}
.btn-back-nav svg { width: 18px; height: 18px; }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.main-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.main-title { font-size: var(--text-md); font-weight: 600; }
.main-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
}

/* Card */
.card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow-sm);
}

.card.card-primary {
  border-color: rgba(217,119,6,0.12);
  box-shadow: 0 2px 8px rgba(0,0,0,0.3), 0 0 1px rgba(217,119,6,0.06);
}
.card-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
  font-size: var(--text-md);
  font-weight: 600;
}
.pill { font-size: var(--text-sm); color: var(--text-muted); font-family: ui-monospace, monospace; }

/* Form */
.row { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: flex-end; }
.field { min-width: 180px; flex: 1; }
label {
  display: block;
  font-size: var(--text-sm);
  color: var(--text-muted);
  margin-bottom: 0.375rem;
}
input, select {
  width: 100%;
  padding: 0.625rem 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-base);
  font-size: var(--text-base);
  color: var(--text-primary);
  transition: border-color 0.15s, background-color 0.15s;
}
input::placeholder { color: var(--text-muted); }
input:focus, select:focus {
  outline: none;
  border-color: var(--border-focus);
  background: var(--bg-tertiary);
}
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:focus-visible,
.btn:focus-visible,
.nav-item:focus-visible,
.mobile-tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.sr-only { position:absolute; width:1px; height:1px; padding:0; margin:-1px; overflow:hidden; clip:rect(0,0,0,0); white-space:nowrap; border:0; }
select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23707070' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2rem;
}
.btns { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
button {
  border: 0;
  border-radius: var(--radius-base);
  padding: 0.625rem 1rem;
  font-weight: 600;
  cursor: pointer;
  font-size: var(--text-base);
  transition: background-color 0.15s, color 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn-primary { background: var(--accent); color: var(--bg-primary); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: var(--bg-tertiary); color: var(--text-primary); }
.btn-ghost:hover { background: var(--border); }
.btn-link { background: transparent; color: var(--text-primary); border: 1px solid var(--border); }
.btn-link:hover { background: var(--bg-tertiary); }
.btn-primary:disabled, .btn-ghost:disabled, .btn-link:disabled, .btn-danger:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}
/* r7.3: destructive actions get solid red filled — distinct from amber primary.
   Single-color amber overload meant "delete" looked like any other CTA; this
   gives destructive actions their own visual category. */
.btn-danger { background: var(--error, #dc2626); color: #fff; border: 1px solid var(--error, #dc2626); }
.btn-danger:hover { background: #b91c1c; border-color: #b91c1c; }
.btn-sm { padding: 0.375rem 0.75rem; font-size: var(--text-sm); }
.btn-primary:active, .btn-ghost:active, .btn-link:active, .btn-danger:active { transform: scale(0.97); }

/* Retry error */
.retry-error {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 1rem;
  color: var(--text-muted);
  font-size: var(--text-sm);
}
.retry-btn {
  background: var(--bg-tertiary);
  color: var(--accent);
  border: 1px solid var(--accent);
  min-height: 36px;
}
.retry-btn:hover { background: rgba(217,119,6,0.1); }

/* Skeleton loading */
@keyframes shimmer {
  0%   { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}
.skeleton {
  background: linear-gradient(90deg, var(--bg-tertiary) 25%, #444 50%, var(--bg-tertiary) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}
.skeleton-line {
  height: 0.875rem;
  margin-bottom: 0.5rem;
}
.skeleton-line.short { width: 60%; }
.skeleton-line.medium { width: 80%; }
.skeleton-chart-item {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  padding: 0.5rem 0.75rem;
}

/* Chips */
.kv { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; margin-top: 0.75rem; }
.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.375rem 0.625rem;
  border-radius: 999px;
  background: var(--chip-bg);
  border: 1px solid var(--chip-bd);
  color: var(--chip-ink);
  font-size: var(--text-sm);
  font-weight: 500;
  line-height: 1;
  white-space: nowrap;
}
.chip.soft { background: var(--bg-tertiary); }
.chip.mono { font-family: ui-monospace, monospace; }
.yao-group { display: flex; align-items: center; gap: 0.5rem; }
/* Semantic chip colors — 五行 */
.chip.el-金 { color: #e8e8e8; border-color: rgba(255,255,255,.25); }
.chip.el-木 { color: #6ee7b7; border-color: rgba(110,231,183,.3); }
.chip.el-水 { color: #7dd3fc; border-color: rgba(125,211,252,.3); }
.chip.el-火 { color: #fca5a5; border-color: rgba(252,165,165,.3); }
.chip.el-土 { color: #fcd34d; border-color: rgba(252,211,77,.3); }
/* 六亲 */
.chip.lq { color: #fbbf24; border-color: rgba(251,191,36,.3); }
/* 六神 */
.chip.ls { color: #d8b4fe; border-color: rgba(216,180,254,.3); }
/* 长生 stage */
.chip.cs { color: #67e8f9; border-color: rgba(103,232,249,.3); }
/* 空亡 */
.chip.kw { color: #9ca3af; border-color: rgba(156,163,175,.3); }
/* 进神 */
.chip.jin { color: #86efac; border-color: rgba(134,239,172,.3); }
/* 退神 */
.chip.tui { color: #fda4af; border-color: rgba(253,164,175,.3); }
.ok { color: var(--success); font-weight: 700; }
.bad { color: var(--error); font-weight: 700; }
.note { margin-top: 0.75rem; color: var(--text-muted); font-size: var(--text-sm); line-height: 1.5; }
.hide { display: none !important; }

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}
.modal.show { display: flex; animation: modalBgFadeIn 0.2s ease; }
.modal.show .modal-content {
  animation: modalSlideIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes modalBgFadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes modalSlideIn {
  from { opacity: 0; transform: translateY(24px) scale(0.95); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: 400px;
  width: 90%;
}
.modal-content h3 {
  margin: 0 0 1rem 0;
  font-size: 1.125rem;
  font-weight: 600;
}
.modal-content p {
  color: var(--text-secondary);
  font-size: var(--text-base);
  margin-bottom: 1rem;
}
.confirm-info {
  background: var(--bg-tertiary);
  border-radius: var(--radius-base);
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
}
.confirm-info div {
  display: flex;
  justify-content: space-between;
  padding: 0.375rem 0;
  font-size: var(--text-base);
  border-bottom: 1px solid var(--border);
}
.confirm-info div:last-child { border-bottom: none; }
.confirm-info span { color: var(--text-primary); font-weight: 500; }
.modal-buttons {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

/* Language selector */
.lang-select {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.375rem 0.625rem;
  font-size: var(--text-sm);
  color: var(--text-secondary);
  cursor: pointer;
  margin-right: 0.5rem;
}
.lang-select:focus {
  outline: none;
  border-color: var(--accent);
}
.lang-select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Container query context for main content area */
.main { container-type: inline-size; container-name: main-content; }

/* Responsive — tablet (769-1024px): sidebar rail mode */
@media (min-width: 769px) and (max-width: 1024px) {
  .sidebar {
    width: var(--sidebar-rail-width);
    overflow: hidden;
  }
  .sidebar-header { padding: 0.75rem 0.5rem; }
  .sidebar-logo span { display: none; }
  .sidebar-logo { justify-content: center; }
  .sidebar-nav { padding: 0.75rem 0.5rem; }
  .nav-item {
    justify-content: center;
    padding: 0.75rem;
  }
  .nav-item span { display: none; }
  .nav-item svg { width: 22px; height: 22px; }
  .sidebar-footer { padding: 0.5rem; }
  .user-details { display: none; }
  .nav-user { justify-content: center; padding: 0.5rem; }
}

/* Responsive — mobile (≤768px): hide sidebar, show bottom tabs */
@media (max-width: 768px) {
  .sidebar { display: none; }

  .main-content {
    padding: 1rem 1rem calc(56px + env(safe-area-inset-bottom, 0px) + 1rem) 1rem !important;
  }

  input, select {
    min-height: 44px;
    font-size: 16px; /* prevent iOS Safari auto-zoom on focus */
  }
  button {
    min-height: 44px;
  }

  body {
    padding-top: env(safe-area-inset-top, 0px);
  }
  #inputCard > .card-title { display: none; }
}

/* Toast notifications */
.toast-container {
  position: fixed;
  top: env(safe-area-inset-top, 0px);
  left: 0;
  right: 0;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  padding: 0.75rem;
  gap: 0.5rem;
}
.toast {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.75rem 1.25rem;
  color: var(--text-primary);
  font-size: var(--text-base);
  line-height: 1.4;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  pointer-events: auto;
  animation: toastIn 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 90%;
  text-align: center;
}
.toast.toast-error {
  border-color: var(--error);
  color: var(--error);
}
.toast.toast-success {
  border-color: var(--success);
  color: var(--success);
}
.toast.toast-out {
  animation: toastOut 0.2s ease forwards;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(-12px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes toastOut {
  from { opacity: 1; transform: translateY(0); }
  to   { opacity: 0; transform: translateY(-12px); }
}

/* Desktop card hover */
@media (hover: hover) {
  .card { transition: transform 0.2s ease, box-shadow 0.2s ease; }
  .card:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,0.4); }
}

/* ── App Footer ──────────────────────────────────────────────────────────── */
.app-footer {
  padding: 1.5rem 1rem;
  text-align: center;
  border-top: 1px solid var(--border);
  margin-top: 2rem;
}
.footer-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 0.5rem;
}
.footer-links a {
  color: var(--text-muted);
  text-decoration: none;
  font-size: var(--text-sm);
}
.footer-links a:hover { color: var(--accent); }
.footer-disclaimer {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Accessibility: respect user's motion preference */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
