:root {
  --bg: #0f1419;
  --fg: #e6e8eb;
  --muted: #8a93a3;
  --accent: #4f8cff;
  --card: #1a1f29;
  --border: #2a3441;
  --danger: #ff6b6b;
  --warn: #ffb84f;
  --ok: #4fde8e;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.5 system-ui, -apple-system, "Noto Sans TC", sans-serif;
  display: grid;
  grid-template-areas: "top top" "side main";
  grid-template-columns: 240px 1fr;
  grid-template-rows: 56px 1fr;
  /* Fixed app shell: viewport-tall so the 1fr main row is bounded and inner
     panes (tags columns, editors, tables) scroll internally instead of the
     whole page. */
  height: 100vh;
  overflow: hidden;
}
body.unauth { display: block; }

.overlay {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg); z-index: 100;
}
.overlay .card {
  background: var(--card); padding: 32px; border-radius: 8px;
  max-width: 420px; width: 90%; box-shadow: 0 4px 20px rgba(0,0,0,.3);
  text-align: center;
}
.overlay h1 { margin: 0 0 16px; font-size: 22px; }
.overlay p { color: var(--muted); }
.overlay .hint { font-size: 12px; margin-top: 24px; }
.overlay .error { color: var(--danger); margin-top: 12px; font-size: 13px; }
#g_id_onload, .g_id_signin { margin: 16px auto; display: inline-block; }

.topbar {
  grid-area: top;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px;
}
.brand { font-weight: 600; font-size: 16px; }
.si-pick { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 6px; margin-left: 16px; margin-right: auto; }
.si-pick select { background: var(--bg, #0a0e14); color: var(--fg); border: 1px solid var(--border); border-radius: 4px; padding: 4px 8px; font-size: 13px; min-height: 30px; }
.user { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 12px; }
.user img { width: 28px; height: 28px; border-radius: 50%; }
.user button { background: none; border: 1px solid var(--border); color: var(--fg); padding: 4px 10px; border-radius: 4px; cursor: pointer; }
.sidebar {
  grid-area: side;
  background: var(--card);
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
}
.sidebar h3 { font-size: 11px; text-transform: uppercase; color: var(--muted); margin: 0 0 8px; letter-spacing: 1px; }
.filelist { list-style: none; padding: 0; margin: 0; }
.filelist li { padding: 8px 10px; border-radius: 4px; cursor: pointer; margin-bottom: 2px; }
.filelist li:hover { background: var(--border); }
.filelist li.active { background: var(--accent); color: #fff; }
.filelist .meta { font-size: 11px; color: var(--muted); }
.filelist li.active .meta { color: rgba(255,255,255,.7); }
.main { grid-area: main; padding: 20px; overflow: auto; min-height: 0; min-width: 0; }
.empty { color: var(--muted); padding: 40px; text-align: center; }
.hidden { display: none !important; }

.editor-head { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.editor-head h2 { margin: 0; font-size: 18px; }
.editor-meta { color: var(--muted); font-size: 12px; font-family: monospace; }
.editor-actions { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.tabs { display: flex; gap: 4px; margin-right: auto; }
.tab { background: transparent; border: 1px solid var(--border); color: var(--fg); padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 13px; }
.tab.active { background: var(--border); }
.btn { background: var(--border); border: none; color: var(--fg); padding: 8px 16px; border-radius: 4px; cursor: pointer; font-size: 13px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn:hover { filter: brightness(1.15); }
.pane { background: var(--card); border: 1px solid var(--border); border-radius: 6px; padding: 12px; }
textarea#raw {
  width: 100%;
  min-height: 60vh;
  background: #0a0e14;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 12px;
  font: 13px/1.5 "JetBrains Mono", "Courier New", monospace;
  resize: vertical;
}
.rows { width: 100%; border-collapse: collapse; font-size: 13px; }
.rows th, .rows td { border-bottom: 1px solid var(--border); padding: 8px; text-align: left; vertical-align: top; }
.rows th { color: var(--muted); font-weight: 500; font-size: 11px; text-transform: uppercase; }
.rows .disabled td { color: var(--muted); text-decoration: line-through; }
.rows .unparsed td { background: rgba(255,107,107,.08); }
.rows code { font-family: "JetBrains Mono", monospace; font-size: 12px; }
.hint { color: var(--muted); font-size: 12px; }

/* Editable api_key table */
.rows-toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.table-scroll { overflow-x: auto; }
.rows.editable td { padding: 4px 6px; vertical-align: middle; }
.rows.editable input[type="text"] {
  background: #0a0e14;
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 5px 6px;
  border-radius: 3px;
  font: 12px/1.4 "JetBrains Mono", monospace;
  width: 100%;
  min-width: 80px;
}
.rows.editable input.w-mac { min-width: 150px; }
.rows.editable input.w-ivmid { min-width: 130px; }
.rows.editable input.w-tags { min-width: 200px; }
.rows.editable input.w-key { min-width: 170px; }
.rows.editable input.w-si, .rows.editable input.w-level { min-width: 64px; }
.rows.editable tr.disabled input { opacity: .55; }
.rows.editable tr.readonly td { color: var(--muted); font-family: "JetBrains Mono", monospace; font-size: 12px; background: rgba(255,255,255,.02); }
.rows.editable .row-del { background: transparent; border: 1px solid var(--danger); color: var(--danger); padding: 3px 8px; border-radius: 3px; cursor: pointer; font-size: 12px; }
.rows.editable .row-del:hover { background: rgba(255,107,107,.12); }
.rows.editable td.enable-cell { text-align: center; }

/* Tags & programs page — fills the viewport, no page-level scroll.
   header (fixed) + workspace (3 columns, each scrolls internally). */
#tagsView { height: 100%; display: flex; flex-direction: column; min-height: 0; }
.tags-header { flex: 0 0 auto; display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.tags-header h2 { margin: 0; font-size: 18px; white-space: nowrap; }
.tags-header .hint { margin-right: auto; }
.tags-workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 240px 200px 1fr;
  gap: 14px;
}
.tags-col, .tags-editor-col {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.col-head { flex: 0 0 auto; margin-bottom: 8px; }
.col-head h3 { margin: 0 0 8px; font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: 1px; }
.add-inline { display: flex; gap: 6px; }
.add-inline input {
  flex: 1 1 auto; min-width: 0;
  background: #0a0e14; border: 1px solid var(--border); color: var(--fg);
  padding: 6px 8px; border-radius: 4px; font: 12px/1.4 system-ui, sans-serif;
}
.scroll-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; margin: 0; }
.filelist.compact li { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.filelist.compact li .tagcnt { font-size: 11px; color: var(--muted); font-family: "JetBrains Mono", monospace; white-space: nowrap; }
.filelist.compact li.active .tagcnt { color: rgba(255,255,255,.7); }

.editor-panel { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.panel-head { flex: 0 0 auto; display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.panel-head h3 { margin: 0; font-size: 16px; }
.panel-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.panel-actions .hint { margin-right: auto; }
.tags-editor-col .empty { margin: auto; }
.noflex { flex: 0 0 auto; margin: 8px 0 0; }
#progRaw {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  background: #0a0e14;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 12px;
  font: 13px/1.5 "JetBrains Mono", "Courier New", monospace;
  resize: none;
}
.checks { flex: 1 1 auto; min-height: 0; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 6px; align-content: start; }
.checks label { display: flex; align-items: center; gap: 6px; font-size: 13px; padding: 4px 6px; border-radius: 4px; }
.checks label:hover { background: var(--border); }
.checks input { accent-color: var(--accent); }
.btn.danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
.btn.danger:hover { background: rgba(255,107,107,.12); }

@media (max-width: 820px) {
  .tags-workspace { grid-template-columns: 1fr 1fr; grid-auto-rows: minmax(0, 1fr); }
  .tags-editor-col { grid-column: 1 / -1; }
}

/* ====== Mobile (≤768px): sidebar 變抽屜、版型單欄、觸控放大 ====== */
.menu-btn {
  display: none;
  background: none; border: 1px solid var(--border);
  color: var(--fg);
  width: 40px; height: 40px;
  border-radius: 4px;
  font-size: 20px; line-height: 1;
  cursor: pointer;
  padding: 0;
}
.menu-btn:hover { background: var(--border); }
.sidebar-backdrop {
  display: none;
  position: fixed; top: 56px; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,.55);
  z-index: 49;
}
body.drawer-open .sidebar-backdrop { display: block; }

@media (max-width: 768px) {
  body {
    grid-template-areas: "top" "main";
    grid-template-columns: 1fr;
    font-size: 14px;
  }
  .menu-btn { display: inline-flex; align-items: center; justify-content: center; }
  .topbar { padding: 0 10px; gap: 8px; }
  .topbar .user { font-size: 12px; }
  .topbar .user img { width: 24px; height: 24px; }
  .topbar .user button { padding: 6px 8px; font-size: 12px; }
  .brand { font-size: 15px; flex: 1; min-width: 0; }
  #publishGoBtn { padding: 8px 10px; font-size: 13px; white-space: nowrap; }
  .main { padding: 12px; }

  /* Sidebar 變左側抽屜，預設藏，open 時滑出 */
  .sidebar {
    position: fixed;
    top: 56px; left: 0; bottom: 0;
    width: 82vw; max-width: 320px;
    z-index: 50;
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 6px 0 24px rgba(0,0,0,.5);
  }
  body.drawer-open .sidebar { transform: translateX(0); }
  .filelist li { padding: 12px 10px; }  /* 觸控目標放大 */

  /* Tags workspace 改單欄 */
  .tags-workspace { grid-template-columns: 1fr !important; grid-auto-rows: auto !important; gap: 10px !important; }
  .tags-col, .tags-editor-col { min-height: 200px; }

  /* 按鈕觸控目標 ≥38px */
  .btn { min-height: 38px; padding: 8px 12px; }
  .btn.small { min-height: 32px; padding: 6px 10px; }

  /* 編輯器 actions row 折行 */
  .editor-actions { flex-wrap: wrap; gap: 6px; }
  .tabs { margin-right: 0; }

  /* api_key_v3.txt 結構化表格輸入框放大 */
  .rows.editable input[type="text"] { min-height: 36px; font-size: 14px; padding: 6px 8px; }
  .rows.editable td { padding: 6px 4px; }

  /* Tags header 折行 */
  .tags-header { flex-wrap: wrap; gap: 8px; }
  .tags-header h2 { white-space: normal; }
  .tags-header .hint { display: none; }  /* 小螢幕省版面 */

  /* Toast 移到底部中央，避免被 FAB 蓋 */
  .toast { right: 12px; left: 12px; bottom: 12px; max-width: none; }
}

@media (max-width: 480px) {
  .brand { font-size: 14px; }
  #publishGoBtn { font-size: 12px; padding: 8px; }
  .filelist li { padding: 14px 10px; font-size: 14px; }
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  padding: 12px 18px;
  border-radius: 4px;
  box-shadow: 0 4px 16px rgba(0,0,0,.4);
  max-width: 400px;
  z-index: 200;
}
.toast.error { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--ok); }
