/* ============================================================
   Pui-Router — Apple / iOS design language (HIG-inspired)
   SF Pro stack, system colors, grouped lists, segmented
   controls, translucent nav, calm motion.
   ============================================================ */

:root {
  --font: -apple-system, "SF Pro Text", "SF Pro Display", "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
  --mono: "SF Mono", "Cascadia Code", "Consolas", ui-monospace, monospace;
  --radius: 14px;
  --ease: cubic-bezier(0.32, 0.72, 0, 1);
  --transition: 0.25s var(--ease);
}

[data-theme="dark"] {
  --bg: #0c0c0e;
  --bg-soft: #1c1c1e;
  --surface: #1c1c1e;
  --surface-hover: #2c2c2e;
  --border: rgba(84, 84, 88, 0.5);
  --separator: rgba(84, 84, 88, 0.36);
  --text: #f5f5f7;
  --text-dim: #98989f;
  --text-tertiary: #636366;
  --accent: #0a84ff;
  --accent-2: #30d158;
  --accent-glow: rgba(10, 132, 255, 0.28);
  --danger: #ff453a;
  --ok: #30d158;
  --fill: rgba(120, 120, 128, 0.16);
  --fill-hover: rgba(120, 120, 128, 0.24);
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  --code-bg: #1c1c1e;
  --nav-bg: rgba(12, 12, 14, 0.72);
  --card-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

[data-theme="light"] {
  --bg: #f2f2f7;
  --bg-soft: #ffffff;
  --surface: #ffffff;
  --surface-hover: #f7f7f9;
  --border: rgba(60, 60, 67, 0.16);
  --separator: rgba(60, 60, 67, 0.18);
  --text: #1c1c1e;
  --text-dim: #6e6e73;
  --text-tertiary: #8e8e93;
  --accent: #007aff;
  --accent-2: #34c759;
  --accent-glow: rgba(0, 122, 255, 0.2);
  --danger: #ff3b30;
  --ok: #34c759;
  --fill: rgba(120, 120, 128, 0.12);
  --fill-hover: rgba(120, 120, 128, 0.2);
  --shadow: 0 16px 48px rgba(30, 35, 60, 0.14);
  --code-bg: #f5f6f8;
  --nav-bg: rgba(242, 242, 247, 0.82);
  --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background var(--transition), color var(--transition);
  overflow-x: hidden;
}

.hidden { display: none !important; }

/* ===== Top bar (floating translucent pill) ===== */
.topbar {
  position: sticky;
  top: 14px;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  width: min(1180px, calc(100% - 32px));
  margin: 14px auto 0;
  padding: 10px 16px;
  border-radius: 18px;
  background: var(--nav-bg);
  backdrop-filter: blur(22px) saturate(1.5);
  -webkit-backdrop-filter: blur(22px) saturate(1.5);
  border: 1px solid var(--separator);
  box-shadow: var(--shadow);
}

.brand { display: flex; align-items: center; gap: 11px; }

.logo {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 10px;
  color: #fff;
  background: var(--accent);
  box-shadow: 0 2px 8px var(--accent-glow);
  transition: var(--transition);
}
.logo:hover { transform: scale(1.05); }

.brand h1 { font-size: 1.05rem; font-weight: 700; letter-spacing: -0.3px; }
.tagline { font-size: 0.68rem; color: var(--text-tertiary); letter-spacing: 0.4px; }

.nav { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.nav-right { display: flex; align-items: center; gap: 8px; }

/* ===== Segmented control (iOS) ===== */
.segmented {
  display: inline-flex;
  gap: 2px;
  padding: 2.5px;
  border-radius: 10px;
  background: var(--fill);
}
.seg-item {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.84rem;
  font-weight: 500;
  padding: 7px 15px;
  border-radius: 8px;
  cursor: pointer;
  transition: var(--transition);
  white-space: nowrap;
}
.seg-item.active {
  background: var(--surface);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18), 0 0 0 0.5px var(--separator);
}
.segmented.small .seg-item { padding: 5px 12px; font-size: 0.78rem; }

/* ===== Chips & icon buttons ===== */
.balance-chip {
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--fill);
  font-family: var(--mono);
  font-weight: 600;
  font-size: 0.82rem;
  color: var(--ok);
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px 4px 4px;
  border-radius: 999px;
  background: var(--fill);
  font-size: 0.84rem;
  font-weight: 500;
}
.avatar {
  width: 27px; height: 27px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  overflow: hidden;
  cursor: pointer;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.icon-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: none;
  background: var(--fill);
  color: var(--text);
  cursor: pointer;
  display: grid; place-items: center;
  transition: var(--transition);
}
.icon-btn:hover { background: var(--fill-hover); }
.icon-btn:active { transform: scale(0.92); }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: none; }

.theme-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--fill);
  color: var(--text);
  cursor: pointer;
  display: grid; place-items: center;
  transition: var(--transition);
}
.theme-btn:hover { background: var(--fill-hover); }

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 22px;
  border-radius: 11px;
  font-size: 0.92rem;
  font-weight: 600;
  font-family: inherit;
  text-decoration: none;
  border: none;
  cursor: pointer;
  transition: var(--transition);
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-secondary { background: var(--fill); color: var(--text); }
.btn-secondary:hover { background: var(--fill-hover); }
.btn-danger {
  background: transparent;
  color: var(--danger);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--danger) 40%, transparent);
}
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
.btn-sm { padding: 7px 14px; font-size: 0.82rem; border-radius: 9px; }
.btn-block { width: 100%; }

/* ===== Layout ===== */
main { max-width: 1000px; margin: 0 auto; padding: 0 clamp(16px, 5vw, 48px); }

.section { padding: 36px 0 56px; }

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.section-head h2 { font-size: 1.65rem; font-weight: 700; letter-spacing: -0.6px; }

/* ===== Hero ===== */
.hero { text-align: center; padding: clamp(36px, 7vh, 64px) 0 28px; }

.hero h2 {
  font-size: clamp(1.9rem, 5.5vw, 2.9rem);
  line-height: 1.12;
  font-weight: 800;
  letter-spacing: -1.2px;
}

.grad { color: var(--accent); }

.hero p {
  max-width: 560px;
  margin: 16px auto 0;
  color: var(--text-dim);
  line-height: 1.6;
  font-size: 0.98rem;
}

.stats {
  display: flex;
  justify-content: center;
  gap: clamp(28px, 6vw, 64px);
  margin-top: 32px;
  flex-wrap: wrap;
}
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat span { font-size: 1.5rem; font-weight: 700; font-family: var(--mono); letter-spacing: -0.5px; }
.stat label { font-size: 0.76rem; color: var(--text-tertiary); }

/* ===== Search field ===== */
.search-field {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--fill);
  color: var(--text-tertiary);
  min-width: 220px;
}
.search-field input {
  border: none;
  background: transparent;
  outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 0.88rem;
  width: 100%;
}
.search-field input::placeholder { color: var(--text-tertiary); }

/* ===== Model list (OpenRouter-like grouped list) ===== */
.model-list {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  overflow: hidden;
}

.model-row {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 16px 20px;
  transition: background var(--transition);
  animation: rowIn 0.4s var(--ease) both;
}
.model-row + .model-row { box-shadow: inset 0 1px 0 var(--separator); }
.model-row:hover { background: var(--surface-hover); }

.model-info { flex: 1; min-width: 0; }
.model-name {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 600;
  font-size: 0.98rem;
  letter-spacing: -0.2px;
}
.model-sub { margin-top: 3px; font-size: 0.78rem; color: var(--text-tertiary); }

.model-price { text-align: right; display: flex; flex-direction: column; gap: 2px; }
.price-main { font-family: var(--mono); font-weight: 600; font-size: 0.9rem; }
.price-unit { font-size: 0.7rem; color: var(--text-tertiary); }

@keyframes rowIn { from { opacity: 0; transform: translateY(10px); } }

/* ===== Badges ===== */
.badge {
  display: inline-block;
  padding: 2.5px 10px;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.badge.free { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.badge.admin { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
.badge.user { background: var(--fill); color: var(--text-dim); }
.badge.ok-badge { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.badge.spend-badge { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }

/* ===== Cards ===== */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 16px;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--card-shadow);
  transition: var(--transition);
  animation: rowIn 0.4s var(--ease) both;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }

.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.card-head h3 { font-size: 1.02rem; font-weight: 600; letter-spacing: -0.2px; }

.status { display: inline-flex; align-items: center; gap: 6px; font-size: 0.74rem; color: var(--text-dim); }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--danger); }
.status.ok::before { background: var(--ok); }

.card-url {
  font-family: var(--mono);
  font-size: 0.76rem;
  color: var(--text-dim);
  background: var(--fill);
  padding: 8px 12px;
  border-radius: 9px;
  margin: 10px 0 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-actions { display: flex; gap: 8px; }

.settings-card { max-width: 460px; }

.switch { position: relative; display: inline-block; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; inset: 0; cursor: pointer;
  background: var(--fill); border-radius: 999px;
  transition: var(--transition);
}
.switch .slider::before {
  content: ""; position: absolute; width: 27px; height: 27px;
  left: 2px; top: 2px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25);
  transition: var(--transition);
}
.switch input:checked + .slider { background: var(--ok); }
.switch input:checked + .slider::before { transform: translateX(20px); }
.switch input:disabled + .slider { opacity: 0.5; cursor: default; }

.empty {
  text-align: center;
  padding: 44px;
  color: var(--text-tertiary);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
}

/* ===== Stats / Analytics ===== */
.totals-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin-bottom: 18px;
}
@media (max-width: 640px) { .totals-row { grid-template-columns: 1fr; } }

.total-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  animation: rowIn 0.4s var(--ease) both;
}
.total-card label { font-size: 0.76rem; color: var(--text-tertiary); }
.total-card strong { font-size: 1.55rem; font-weight: 700; font-family: var(--mono); letter-spacing: -0.8px; }

.chart-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 20px;
  margin-bottom: 18px;
  animation: rowIn 0.45s var(--ease) both;
}
.chart-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 0.92rem;
  font-weight: 600;
  margin-bottom: 14px;
}

.legend {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.74rem;
  color: var(--text-dim);
}
.lg { width: 9px; height: 9px; border-radius: 50%; display: inline-block; margin-left: 10px; }
.lg.in { background: var(--accent); margin-left: 0; }
.lg.out { background: var(--ok); }

.chart { width: 100%; }
.chart-svg { width: 100%; height: 190px; display: block; overflow: visible; }
.chart-grid { stroke: var(--separator); stroke-width: 1; vector-effect: non-scaling-stroke; }
.chart-line {
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: drawLine 1.1s var(--ease) forwards;
}
.chart-area { opacity: 0; animation: areaIn 0.9s ease 0.3s both; }
.chart-dot { opacity: 0; animation: fadeIn 0.4s ease 0.9s both; }
@keyframes drawLine { to { stroke-dashoffset: 0; } }
@keyframes fadeIn { to { opacity: 1; } }
@keyframes areaIn { to { opacity: 0.1; } }

.chart-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-size: 0.7rem;
  font-family: var(--mono);
  color: var(--text-tertiary);
}

/* ===== Tables ===== */
.table-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  overflow: auto;
  animation: rowIn 0.45s var(--ease) both;
}

.table-title {
  padding: 16px 20px;
  font-weight: 600;
  font-size: 0.92rem;
  box-shadow: inset 0 -1px 0 var(--separator);
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  min-width: 560px;
}
.table th {
  text-align: left;
  font-size: 0.72rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-tertiary);
  padding: 12px 20px;
  box-shadow: inset 0 -1px 0 var(--separator);
}
.table td {
  padding: 13px 20px;
  box-shadow: inset 0 -1px 0 var(--separator);
}
.table tr:last-child td { box-shadow: none; }
.table tr:hover td { background: var(--surface-hover); }
.table .muted, td.muted { text-align: center; color: var(--text-tertiary); }

.row-actions { display: flex; gap: 8px; justify-content: flex-end; }

td.plus { color: var(--ok); font-family: var(--mono); font-weight: 600; }
td.minus { color: var(--danger); font-family: var(--mono); font-weight: 600; }

.key-val { font-family: var(--mono); font-size: 0.78rem; color: var(--accent); }

/* ===== Playground ===== */
.chat-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  overflow: hidden;
}

.chat-controls {
  display: flex;
  gap: 10px;
  padding: 14px 18px;
  box-shadow: inset 0 -1px 0 var(--separator);
  flex-wrap: wrap;
}

select {
  flex: 1;
  min-width: 160px;
  padding: 10px 34px 10px 14px;
  border-radius: 10px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.88rem;
  cursor: pointer;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' fill='none' stroke='%238e8e93' stroke-width='1.5'%3E%3Cpath d='M1 1l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
}

/* ===== Animated model picker ===== */
.model-picker { position: relative; flex: 1; min-width: 180px; }
.mp-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  border-radius: 10px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
}
.mp-btn:hover { background: var(--fill-hover); }
.mp-chev { transition: transform 0.3s var(--ease); flex-shrink: 0; }
.model-picker.open .mp-chev { transform: rotate(180deg); }
#mpLabel { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#mpLabel.swap { animation: labelSwap 0.32s var(--ease); }
@keyframes labelSwap {
  0% { opacity: 0; transform: translateY(6px); }
  100% { opacity: 1; transform: translateY(0); }
}
.mp-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  max-height: 300px;
  overflow-y: auto;
  padding: 6px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--separator);
  box-shadow: var(--shadow);
  z-index: 40;
  opacity: 0;
  transform: translateY(-8px) scale(0.98);
  transform-origin: top center;
  pointer-events: none;
  transition: opacity 0.22s var(--ease), transform 0.22s var(--ease);
}
.model-picker.open .mp-menu {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}
.mp-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.86rem;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s var(--ease);
  animation: mpItemIn 0.3s var(--ease) both;
}
.mp-item:hover { background: var(--fill); }
.mp-item.sel { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.mp-item-name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mp-item-price { font-family: var(--mono); font-size: 0.74rem; color: var(--text-tertiary); }
.mp-check { opacity: 0; color: var(--accent); flex-shrink: 0; transition: opacity 0.2s var(--ease); }
.mp-item.sel .mp-check { opacity: 1; }
@keyframes mpItemIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Language button ===== */
.lang-btn {
  min-width: 38px;
  height: 34px;
  padding: 0 10px;
  border-radius: 999px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.3px;
  cursor: pointer;
  transition: var(--transition);
}
.lang-btn:hover { background: var(--fill-hover); }
.lang-btn:active { transform: scale(0.92); }

.chat-log {
  height: 400px;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.msg {
  max-width: 76%;
  padding: 11px 15px;
  border-radius: 18px;
  line-height: 1.5;
  font-size: 0.92rem;
  white-space: pre-wrap;
  word-break: break-word;
  animation: msgIn 0.35s var(--ease) both;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 6px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--fill);
  color: var(--text);
  border-bottom-left-radius: 6px;
}
.msg.error {
  align-self: flex-start;
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  color: var(--danger);
  border-bottom-left-radius: 6px;
}
.msg.hint {
  align-self: center;
  max-width: 100%;
  text-align: center;
  color: var(--text-tertiary);
  background: transparent;
  font-size: 0.84rem;
}

@keyframes msgIn { from { opacity: 0; transform: translateY(8px) scale(0.98); } }

.typing { display: inline-flex; gap: 5px; padding: 4px 2px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-tertiary);
  animation: blink 1.2s infinite;
}
.typing i:nth-child(2) { animation-delay: 0.2s; }
.typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

.chat-input {
  display: flex;
  gap: 10px;
  padding: 14px 18px;
  box-shadow: inset 0 1px 0 var(--separator);
}

textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  padding: 11px 16px;
  border-radius: 20px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.92rem;
  line-height: 1.5;
  outline: none;
  transition: var(--transition);
}
textarea:focus { background: var(--fill-hover); box-shadow: 0 0 0 3px var(--accent-glow); }

.send-btn { width: 42px; height: 42px; padding: 0; border-radius: 50%; flex-shrink: 0; }

.msg-cost {
  margin-top: 7px;
  font-size: 0.7rem;
  color: var(--text-tertiary);
  font-family: var(--mono);
}

/* ===== Playground code blocks ===== */
.msg.bot { white-space: normal; }
.msg-text { white-space: pre-wrap; }
.reasoning {
  margin: 0 0 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--fill);
  overflow: hidden;
}
.reasoning summary {
  cursor: pointer;
  padding: 7px 12px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-dim);
  list-style: none;
  user-select: none;
}
.reasoning summary::-webkit-details-marker { display: none; }
.reasoning summary::before { content: "▸ "; }
.reasoning[open] summary::before { content: "▾ "; }
.reasoning-body {
  padding: 0 12px 10px;
  font-size: 0.82rem;
  line-height: 1.5;
  font-style: italic;
  color: var(--text-dim);
  white-space: pre-wrap;
  word-break: break-word;
}
.code-block {
  margin: 8px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
}
.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: var(--fill);
  border-bottom: 1px solid var(--border);
}
.code-lang {
  font-family: var(--mono);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-dim);
  letter-spacing: 0.4px;
}
.code-actions { display: flex; gap: 6px; }
.code-btn {
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 6px;
  transition: var(--transition);
}
.code-btn:hover { background: var(--accent-glow); }
.code-body {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text);
  white-space: pre;
}

/* ===== HTML preview dialog ===== */
.preview-dialog:not([open]) { display: none; }
.preview-dialog {
  width: min(920px, 94vw);
  height: min(720px, 90vh);
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow: hidden;
}
.preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.preview-head h3 { margin: 0; font-size: 1rem; }
.preview-actions { display: flex; gap: 8px; }
.preview-frame {
  flex: 1;
  width: 100%;
  border: none;
  background: #fff;
}

/* ===== Billing ===== */
.billing-grid {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 18px;
  align-items: start;
}
@media (max-width: 800px) { .billing-grid { grid-template-columns: 1fr; } }

.balance-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 26px;
  animation: rowIn 0.4s var(--ease) both;
}
.balance-label { font-size: 0.78rem; color: var(--text-tertiary); }
.balance-big {
  font-size: 2.5rem;
  font-weight: 700;
  font-family: var(--mono);
  letter-spacing: -1.5px;
  margin: 4px 0 18px;
  color: var(--text);
}

.topup-row { display: flex; gap: 8px; }
.topup-row input {
  flex: 1;
  min-width: 0;
  padding: 11px 14px;
  border-radius: 10px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
  transition: var(--transition);
}
.topup-row input:focus { box-shadow: 0 0 0 3px var(--accent-glow); }

.quick-amounts { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.chip-btn {
  padding: 6px 14px;
  border-radius: 999px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}
.chip-btn:hover { background: var(--fill-hover); }
.chip-btn:active { transform: scale(0.95); }

/* ===== Code cards ===== */
.code-card {
  margin-top: 24px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 22px;
}
.code-card h3 { margin-bottom: 4px; font-size: 1.02rem; }
.code-block {
  margin: 14px 0;
  padding: 16px;
  border-radius: 11px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.65;
  overflow-x: auto;
  color: var(--text-dim);
  white-space: pre;
}

.inline-code {
  font-family: var(--mono);
  font-size: 0.84em;
  background: var(--fill);
  padding: 2px 7px;
  border-radius: 6px;
  color: var(--accent);
}

/* ===== Dialogs ===== */
dialog {
  margin: auto;
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 26px;
  width: min(440px, 92vw);
  box-shadow: var(--shadow);
  animation: dialogIn 0.28s var(--ease);
}
@keyframes dialogIn {
  from { opacity: 0; transform: scale(0.94); }
  to { opacity: 1; transform: scale(1); }
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

dialog h3 { margin-bottom: 18px; font-size: 1.1rem; letter-spacing: -0.3px; }
dialog label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 14px;
}
dialog input {
  width: 100%;
  margin-top: 5px;
  padding: 10px 14px;
  border-radius: 10px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
  transition: var(--transition);
}
dialog input:focus { box-shadow: 0 0 0 3px var(--accent-glow); }

.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

/* ===== iOS alert ===== */
#iosAlert {
  padding: 0;
  width: min(280px, 88vw);
  text-align: center;
  border-radius: 16px;
  overflow: hidden;
}
#iosAlert::backdrop {
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.ios-alert-content { padding: 20px 18px 16px; }
.ios-alert-content h3 { font-size: 0.98rem; font-weight: 600; margin-bottom: 4px; }
.ios-alert-content p { font-size: 0.84rem; color: var(--text-dim); line-height: 1.45; margin: 0; }
.ios-alert-content input {
  margin-top: 14px;
  width: 100%;
  padding: 9px 12px;
  border-radius: 9px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.8rem;
  outline: none;
  text-align: center;
}

.ios-alert-actions { display: flex; box-shadow: inset 0 1px 0 var(--separator); }
.ios-alert-btn {
  flex: 1;
  appearance: none;
  border: none;
  background: transparent;
  color: var(--accent);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 400;
  padding: 13px 6px;
  cursor: pointer;
  transition: background 0.15s ease;
}
.ios-alert-btn + .ios-alert-btn { box-shadow: inset 1px 0 0 var(--separator); }
.ios-alert-btn:active { background: var(--fill); }
.ios-alert-btn.default { font-weight: 600; }
.ios-alert-btn.destructive { color: var(--danger); font-weight: 600; }

/* ===== Toasts ===== */
.toast-host {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.toast {
  padding: 11px 20px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.86rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(14px) scale(0.96);
  transition: 0.32s var(--ease);
}
.toast.show { opacity: 1; transform: translateY(0) scale(1); }
.toast.ok { box-shadow: var(--shadow), inset 0 0 0 1px color-mix(in srgb, var(--ok) 35%, transparent); }
.toast.warn { box-shadow: var(--shadow), inset 0 0 0 1px color-mix(in srgb, #ff9f0a 40%, transparent); }
.toast.err { box-shadow: var(--shadow), inset 0 0 0 1px color-mix(in srgb, var(--danger) 40%, transparent); }

/* ===== Auth ===== */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(1200px 600px at 50% -10%, var(--accent-glow), transparent 60%),
    var(--bg);
}

.auth-card {
  position: relative;
  width: min(380px, 100%);
  background: var(--surface);
  border-radius: 20px;
  padding: 36px 30px 30px;
  text-align: center;
  box-shadow: var(--shadow);
  animation: rowIn 0.5s var(--ease) both;
}

.auth-logo {
  width: 58px; height: 58px;
  margin: 0 auto 16px;
  display: grid; place-items: center;
  border-radius: 15px;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 6px 18px var(--accent-glow);
}

.auth-card h2 { font-size: 1.4rem; margin-bottom: 6px; letter-spacing: -0.5px; }
.auth-sub { color: var(--text-dim); font-size: 0.88rem; margin-bottom: 24px; }

.auth-card form { text-align: left; }
.auth-card label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 14px;
}
.auth-card input {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 11px 14px;
  border-radius: 10px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.92rem;
  outline: none;
  transition: var(--transition);
}
.auth-card input:focus { box-shadow: 0 0 0 3px var(--accent-glow); }

.auth-error {
  min-height: 18px;
  margin-bottom: 8px;
  font-size: 0.8rem;
  color: var(--danger);
}

.auth-switch { margin-top: 18px; font-size: 0.85rem; color: var(--text-dim); }
.auth-switch a { color: var(--accent); text-decoration: none; font-weight: 600; }

.auth-tools {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 20px;
}
.auth-tools .lang-btn { height: 38px; }
.auth-tools .theme-btn { width: 38px; height: 38px; }

/* ===== Footer ===== */
.footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  padding: 28px 16px 40px;
  color: var(--text-tertiary);
  font-size: 0.8rem;
}
.footer .dot { opacity: 0.6; }

/* ===== Responsive ===== */
/* ===== Admin panels ===== */
.admin-panel { margin-bottom: 8px; }
.panel-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.panel-head h3 { font-size: 1.15rem; letter-spacing: -0.3px; }
.panel-sub { font-size: 0.82rem; color: var(--text-tertiary); margin-top: 3px; }
.field-row { display: flex; gap: 12px; }
.field-row label { flex: 1; }
dialog select {
  width: 100%;
  margin-top: 5px;
  padding: 10px 14px;
  border-radius: 10px;
  border: none;
  background: var(--fill);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
}
dialog select:focus { box-shadow: 0 0 0 3px var(--accent-glow); }

/* ===== Burger + drawer ===== */
.burger-btn {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 38px;
  height: 38px;
  padding: 0 9px;
  border: none;
  border-radius: 10px;
  background: var(--fill);
  cursor: pointer;
  flex: none;
}
.burger-btn span {
  display: block;
  height: 2px;
  width: 100%;
  border-radius: 2px;
  background: var(--text);
  transition: var(--transition);
}
.burger-btn:active { transform: scale(0.92); }

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 80;
  height: 100dvh;
  width: min(320px, 86vw);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 22px 16px;
  background: var(--surface);
  border-left: 1px solid var(--separator);
  box-shadow: var(--shadow);
  transform: translateX(100%);
  transition: transform 0.3s var(--ease);
}
.drawer.open { transform: translateX(0); }
.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity 0.3s var(--ease);
}
.drawer-backdrop:not(.hidden) { opacity: 1; }
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 14px;
  margin-bottom: 6px;
  border-bottom: 1px solid var(--separator);
}
.drawer-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.drawer-foot { display: flex; flex-direction: column; gap: 2px; padding-top: 10px; border-top: 1px solid var(--separator); }
.drawer-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.92rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: var(--transition);
}
.drawer-item:hover { background: var(--fill); }
.drawer-item.active { background: var(--fill); font-weight: 600; }
.drawer-item.danger { color: var(--danger); }
.di-ico { font-size: 1.05rem; width: 22px; text-align: center; }
body.no-scroll { overflow: hidden; }

@media (max-width: 860px) {
  .nav-right { display: none; }
  .nav .segmented { display: none; }
  .burger-btn { display: flex; }
  .nav { justify-content: flex-end; }
}

@media (max-width: 860px) {
  .topbar {
    top: 8px;
    width: calc(100% - 16px);
    margin-top: 8px;
    padding: 10px 12px;
    border-radius: 16px;
    gap: 8px;
  }
  .brand h1 { font-size: 0.98rem; }
  .tagline { display: none; }
  .nav {
    order: 2;
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
  }
  .nav-right { order: 1; }
  .segmented {
    order: 2;
    width: 100%;
    overflow-x: auto;
    flex-wrap: nowrap;
    justify-content: flex-start;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .segmented::-webkit-scrollbar { display: none; }
  .seg-item { padding: 7px 13px; }
  main { padding: 0 clamp(12px, 4vw, 24px); }
  .section { padding: 24px 0 40px; }
  .section-head h2 { font-size: 1.35rem; }
  .hero { padding: 24px 0 20px; }
  .hero h2 { font-size: clamp(1.7rem, 7vw, 2.2rem); letter-spacing: -0.8px; }
  .hero p { font-size: 0.92rem; }
  .stats { gap: 28px; margin-top: 24px; }
  .search-field { min-width: 0; width: 100%; }
  .section-head { gap: 10px; }
  .billing-grid { grid-template-columns: 1fr; }
  .chat-log { height: 62vh; padding: 14px; }
  .msg { max-width: 88%; font-size: 0.9rem; }
  .code-card { padding: 16px; }
  .code-block { font-size: 0.72rem; }
  .table th, .table td { padding: 11px 10px; font-size: 0.8rem; }
  .row-actions { flex-direction: column; gap: 6px; }
  .row-actions .btn { width: 100%; padding: 7px 10px; font-size: 0.76rem; }
}

@media (max-width: 480px) {
  .topbar { top: 6px; width: calc(100% - 12px); margin-top: 6px; }
  .brand { gap: 8px; }
  .logo { width: 32px; height: 32px; }
  .user-chip span:last-child { display: none; }
  .user-chip { padding: 4px; }
  .balance-chip { padding: 6px 10px; font-size: 0.76rem; }
  .icon-btn, .lang-btn { height: 30px; width: 30px; min-width: 30px; padding: 0; }
  .lang-btn { font-size: 0.7rem; }
  .model-row { padding: 12px 14px; flex-wrap: wrap; gap: 8px 14px; }
  .model-price { order: 3; width: 100%; text-align: left; flex-direction: row; gap: 8px; align-items: baseline; }
  .totals-row { gap: 10px; }
  .total-card { padding: 14px 16px; }
  .total-card strong { font-size: 1.3rem; }
  .chart-svg { height: 150px; }
  .chart-card { padding: 14px; }
  .chat-controls { padding: 12px 14px; }
  .msg { max-width: 94%; }
  .auth-card { padding: 28px 20px 24px; }
  .quick-amounts .chip-btn { flex: 1; }
}

@media (max-width: 360px) {
  .seg-item { padding: 6px 10px; font-size: 0.8rem; }
  .hero h2 { font-size: 1.55rem; }
}

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