:root {
  --bg: #f6f7f9; --panel: #ffffff; --panel-2: #f1f3f6; --line: #e3e6eb; --text: #14171c; --muted: #667085;
  --accent: #10a37f; --accent-ink: #ffffff; --accent-soft: #e6f6f1; --danger: #d92d20; --danger-soft: #fdecea;
  --warn: #b54708; --warn-soft: #fef3e2; --info: #1d63d6; --info-soft: #e8f0fe;
  --radius: 12px; --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1013; --panel: #16191e; --panel-2: #1d2127; --line: #2a2f37; --text: #e8eaed; --muted: #9aa3af;
    --accent: #19c37d; --accent-ink: #06150f; --accent-soft: #0f2a20; --danger: #f97066; --danger-soft: #2d1513;
    --warn: #fdb022; --warn-soft: #2b2010; --info: #6ea8fe; --info-soft: #14213a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }

/* ── khung ── */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px;
  padding: 0 20px; height: 56px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: -.01em; white-space: nowrap; }
.brand .dot { width: 22px; height: 22px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; color: var(--accent-ink); font-size: 12px; font-weight: 800; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.tab {
  border: 0; background: none; padding: 8px 14px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 550; white-space: nowrap;
}
.tab:hover { background: var(--panel-2); color: var(--text); }
.tab.active { background: var(--accent-soft); color: var(--accent); }
.user { display: flex; align-items: center; gap: 10px; color: var(--muted); white-space: nowrap; }
main { max-width: 1180px; margin: 0 auto; padding: 24px 20px 64px; }
.page-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 0 0 16px; flex-wrap: wrap; }
.page-title h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-body { padding: 18px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.card-head h2 { font-size: 15px; margin: 0; }

/* ── form ── */
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-weight: 550; margin-bottom: 6px; font-size: 13px; }
label.field > small { display: block; color: var(--muted); margin-top: 4px; font-size: 12px; }
input[type=text], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; background: var(--panel); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; min-height: 96px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; font-weight: 550; white-space: nowrap; text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.lg { padding: 11px 18px; font-size: 15px; }

.seg { display: inline-flex; padding: 3px; background: var(--panel-2); border-radius: 10px; gap: 2px; }
.seg button { border: 0; background: none; padding: 7px 14px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 550; }
.seg button.on { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--panel-2); color: var(--muted); }
.badge.ok { background: var(--accent-soft); color: var(--accent); }
.badge.err { background: var(--danger-soft); color: var(--danger); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.info { background: var(--info-soft); color: var(--info); }

/* ── trang Tạo ── */
.create-grid { display: grid; grid-template-columns: minmax(0, 420px) minmax(0, 1fr); gap: 20px; align-items: start; }
@media (max-width: 900px) { .create-grid { grid-template-columns: 1fr; } }
.create-form { position: sticky; top: 76px; }
@media (max-width: 900px) { .create-form { position: static; } }

.drop {
  border: 1.5px dashed var(--line); border-radius: 10px; padding: 14px; text-align: center; color: var(--muted); cursor: pointer;
  transition: border-color .15s, background .15s; font-size: 13px;
}
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; margin-top: 10px; }
.thumbs:empty { display: none; }
.thumb { position: relative; aspect-ratio: 1; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); background: var(--panel-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .idx { position: absolute; left: 4px; top: 4px; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; padding: 0 5px; border-radius: 5px; }
.thumb .x { position: absolute; right: 3px; top: 3px; width: 20px; height: 20px; border: 0; border-radius: 50%; background: rgba(0,0,0,.65); color: #fff; cursor: pointer; line-height: 20px; padding: 0; font-size: 13px; }

.results { display: flex; flex-direction: column; gap: 14px; }
.empty { padding: 48px 20px; text-align: center; color: var(--muted); }
.job .card-head { flex-wrap: wrap; }
.job-prompt { white-space: pre-wrap; word-break: break-word; margin: 0 0 10px; }
.job-meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; color: var(--muted); font-size: 12.5px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.gallery figure { margin: 0; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: var(--panel-2); }
.gallery img { display: block; width: 100%; height: auto; cursor: zoom-in; }
.gallery figcaption { display: flex; gap: 6px; padding: 8px; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); }
.answer { white-space: pre-wrap; word-break: break-word; background: var(--panel-2); border-radius: 10px; padding: 12px 14px; margin: 0; }
.error-box { background: var(--danger-soft); color: var(--danger); border-radius: 10px; padding: 10px 12px; white-space: pre-wrap; word-break: break-word; font-size: 13px; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { aspect-ratio: 1; border-radius: 10px; background: linear-gradient(90deg, var(--panel-2) 25%, var(--line) 50%, var(--panel-2) 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ── Lịch sử ── */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input[type=search] { max-width: 320px; }
.hist { display: flex; flex-direction: column; gap: 10px; }
.hist-item { display: grid; grid-template-columns: 1fr auto; gap: 12px; padding: 14px 16px; }
.hist-item .strip { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.hist-item .strip img { width: 84px; height: 84px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); cursor: zoom-in; }
.hist-item .strip .ref { opacity: .85; width: 48px; height: 48px; }
.hist-item .actions { display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.hist-item .prompt { white-space: pre-wrap; word-break: break-word; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.hist-item .prompt.open { -webkit-line-clamp: unset; }
.hist-item .answer { margin-top: 8px; max-height: 220px; overflow: auto; font-size: 13px; }
@media (max-width: 640px) { .hist-item { grid-template-columns: 1fr; } .hist-item .actions { flex-direction: row; align-items: center; } }

/* ── Tài khoản ── */
.acc-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 380px); gap: 20px; align-items: start; }
@media (max-width: 960px) { .acc-grid { grid-template-columns: 1fr; } }
.acc-list { display: flex; flex-direction: column; gap: 10px; }
.acc { padding: 14px 16px; }
.acc-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.acc-name { font-weight: 650; font-size: 15px; }
.acc-info { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 6px 16px; margin: 10px 0; font-size: 13px; }
.acc-info div > span:first-child { color: var(--muted); margin-right: 6px; }
.acc-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.acc .err-line { font-size: 12.5px; color: var(--danger); word-break: break-word; margin-top: 4px; }
.switch { position: relative; display: inline-block; width: 36px; height: 20px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 20px; cursor: pointer; transition: .15s; }
.switch span::before { content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::before { transform: translateX(16px); }
details.help { margin-top: 6px; font-size: 13px; color: var(--muted); }
details.help summary { cursor: pointer; color: var(--text); font-weight: 550; }
details.help ol { padding-left: 18px; margin: 8px 0 0; }

/* ── lightbox, toast, modal ── */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 50; padding: 24px; cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: 360px; font-size: 13.5px; }
.toast.err { background: var(--danger); color: #fff; }
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal { width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow: auto; }

/* ── đăng nhập ── */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 380px; }
.login .brand { justify-content: center; font-size: 18px; margin-bottom: 6px; }
.login .sub { text-align: center; color: var(--muted); margin: 0 0 22px; }
.login .btn { width: 100%; }

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 16px; gap: 8px; }
  .tabs { order: 3; flex-basis: 100%; }
  .user { margin-left: auto; }
  #who { display: none; }
  main { padding: 16px 16px 48px; }
}
