/* ===== デザイントークン（元アプリ ui/theme.py より） ===== */
:root {
  --bg-app: #16161A;
  --bg-panel: #1E1E24;
  --bg-surface: #26262E;
  --bg-surface-2: #2F2F3A;
  --bg-hover: #33333F;
  --bg-active: #3D3D4E;

  --border: #34343F;
  --border-soft: #2A2A33;
  --divider: #26262E;

  --text: #EAEAEE;
  --text-muted: #9A9AA4;
  --text-dim: #6E6E78;
  --on-dark: #FFFFFF;
  --on-light: #1A1A1F;

  --accent: #7C7FE8;
  --accent-hover: #9598F0;
  --accent-press: #6366E0;

  --success: #34D399;
  --success-hover: #10B981;
  --warning: #F5C518;
  --warning-hover: #E0B016;
  --danger: #F87171;
  --info: #22D3EE;

  --lv-beginner: #10B981;
  --lv-beginner-txt: #D1FAE5;
  --lv-intermediate: #6366F1;
  --lv-intermediate-txt: #E0E7FF;

  --r-sm: 4px; --r-md: 6px; --r-lg: 8px; --r-xl: 12px;

  --header-h: 52px;
  --tabbar-h: 60px;

  --font-ui: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  --font-code: "SF Mono", ui-monospace, Menlo, Consolas, monospace;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg-app);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* ===== ヘッダー ===== */
.app-header {
  flex: 0 0 auto;
  height: calc(var(--header-h) + var(--safe-top));
  padding-top: var(--safe-top);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 16px;
  padding-right: 12px;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand-logo { font-size: 20px; }
.brand-title { font-size: 16px; font-weight: 700; }

/* ===== 画面共通 ===== */
.view {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px;
  padding-bottom: 24px;
}
.view.no-pad { padding: 0; }

.section-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--text-dim);
  margin: 4px 2px 10px;
  text-transform: none;
}

/* ===== タブバー ===== */
.tabbar {
  flex: 0 0 auto;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  display: flex;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-family: inherit;
  cursor: pointer;
  padding: 0;
}
.tab-ico { font-size: 20px; line-height: 1; filter: grayscale(.4) opacity(.8); }
.tab-lbl { font-size: 10px; }
.tab.active { color: var(--accent); }
.tab.active .tab-ico { filter: none; }

/* ===== ボタン ===== */
button { font-family: inherit; }
.primary-btn, .success-btn, .warning-btn, .ghost-btn, .danger-btn {
  border: 1px solid transparent;
  border-radius: var(--r-md);
  padding: 11px 16px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: background .12s, opacity .12s;
}
.primary-btn { background: var(--accent); color: var(--on-dark); }
.primary-btn:active { background: var(--accent-press); }
.success-btn { background: var(--success); color: var(--on-light); }
.warning-btn { background: var(--warning); color: var(--on-light); }
.danger-btn  { background: var(--danger);  color: var(--on-light); }
.ghost-btn {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.ghost-btn:active { background: var(--bg-hover); }
.ghost-btn.small, .primary-btn.small { padding: 7px 12px; font-size: 12px; font-weight: 600; }
.ghost-btn.muted { color: var(--text-muted); }
button:disabled { opacity: .45; cursor: default; }
.btn-full { width: 100%; }

/* ===== カード ===== */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-lg);
  padding: 14px;
  margin-bottom: 12px;
}
.card-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  margin: 0 0 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* ===== フォーム要素 ===== */
.field-label { font-size: 11px; color: var(--text-muted); margin: 0 0 5px; }
.select, .text-input {
  width: 100%;
  background: var(--bg-surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 11px 12px;
  font-size: 15px;
  font-family: inherit;
  appearance: none;
  -webkit-appearance: none;
}
.select {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%239A9AA4' d='M2 4l4 4 4-4z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.row.g2 > .grow2 { flex: 2; }

/* ===== バッジ ===== */
.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 999px;
  vertical-align: middle;
}
.badge.beginner { background: rgba(16,185,129,.18); color: var(--lv-beginner-txt); border: 1px solid var(--lv-beginner); }
.badge.intermediate { background: rgba(99,102,241,.18); color: var(--lv-intermediate-txt); border: 1px solid var(--lv-intermediate); }
.badge.cat { background: var(--bg-surface-2); color: var(--text-muted); }

/* ===== 教科書：トピック一覧 ===== */
.cat-group { margin-bottom: 18px; }
.cat-head { font-size: 13px; font-weight: 700; color: var(--accent); margin: 0 2px 8px; }
.topic-list { display: grid; grid-template-columns: 1fr; gap: 8px; }
.topic-item {
  background: var(--bg-surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-lg);
  padding: 13px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
}
.topic-item:active { background: var(--bg-hover); }
.topic-item .ti-title { font-size: 15px; font-weight: 600; }
.topic-item .ti-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.chevron { color: var(--text-dim); font-size: 18px; }

/* ===== 教科書：本文 ===== */
.tb-detail-head {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 12px;
}
.tb-detail-head .ttl { font-size: 18px; font-weight: 700; }
.textbook-body {
  background: var(--bg-surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-lg);
  padding: 14px;
  font-family: var(--font-code);
  font-size: 13px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
  -webkit-user-select: text;
  user-select: text;
}
.textbook-body::selection { background: rgba(124,127,232,.4); }

.back-btn {
  background: transparent; border: none; color: var(--accent);
  font-size: 15px; font-weight: 600; padding: 6px 0; cursor: pointer;
  display: inline-flex; align-items: center; gap: 4px;
}

/* ===== コードエディタ ===== */
.editor-card { padding: 0; overflow: hidden; }
.editor-head, .panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 12px;
  border-bottom: 1px solid var(--divider);
}
.panel-head .ph-title, .editor-head .eh-title {
  font-size: 12px; font-weight: 700; color: var(--text-muted);
}
.editor-head .eh-actions { display: flex; gap: 8px; }
.CodeMirror {
  height: auto;
  min-height: 180px;
  font-family: var(--font-code);
  font-size: 13.5px;
  line-height: 1.5;
}
.CodeMirror.cm-s-material-darker { background: #1E1E1E; }
.err-line {
  color: var(--danger);
  font-family: var(--font-code);
  font-size: 11px;
  padding: 6px 12px;
  min-height: 12px;
}

/* ===== 出力 / 採点 ===== */
.output-card { padding: 0; overflow: hidden; }
.output-box {
  background: #1E1E1E;
  color: #D4D4D4;
  font-family: var(--font-code);
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  padding: 12px;
  min-height: 90px;
  max-height: 320px;
  overflow-y: auto;
  -webkit-user-select: text;
  user-select: text;
}
.output-box .ok { color: var(--success); }
.output-box .ng { color: var(--danger); }
.output-box .muted { color: var(--text-dim); }

/* ===== 問題文 ===== */
.problem-box { white-space: pre-wrap; word-break: break-word; line-height: 1.65; font-size: 14px; }
.problem-box .p-title { font-size: 16px; font-weight: 700; margin-bottom: 6px; display:block; }
.problem-box .p-sub { color: var(--text-muted); font-size: 12px; font-weight: 700; margin-top: 12px; display:block; }
.req-list { margin: 6px 0 0; padding-left: 20px; }
.req-list li { margin-bottom: 3px; }

/* ===== チャット ===== */
.chat-card { display: flex; flex-direction: column; }
.chat-log {
  max-height: 50vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 10px;
  padding: 4px 0 10px;
}
.msg { padding: 10px 12px; border-radius: var(--r-lg); font-size: 14px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; }
.msg.user { background: var(--accent); color: var(--on-dark); align-self: flex-end; max-width: 88%; }
.msg.assistant { background: var(--bg-surface-2); color: var(--text); align-self: flex-start; max-width: 92%; }
.msg pre { background: #1E1E1E; padding: 10px; border-radius: var(--r-md); overflow-x: auto; font-family: var(--font-code); font-size: 12.5px; }
.msg code { font-family: var(--font-code); }
.chat-input-row { display: flex; gap: 8px; align-items: flex-end; }
.chat-input-row textarea {
  flex: 1; resize: none;
  background: var(--bg-surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-md);
  padding: 10px; font-family: inherit; font-size: 15px; max-height: 120px;
}

/* ===== 保存した問題 ===== */
.saved-item {
  background: var(--bg-surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-lg);
  padding: 13px 14px;
  margin-bottom: 10px;
}
.saved-item .si-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }
.saved-item .si-title { font-size: 15px; font-weight: 600; }
.saved-item .si-desc { font-size: 13px; color: var(--text-muted); line-height: 1.5; margin-bottom: 10px; }
.saved-item .si-meta { font-size: 11px; color: var(--text-dim); }
.saved-actions { display: flex; gap: 8px; }

/* ===== 空状態 ===== */
.empty {
  text-align: center;
  color: var(--text-dim);
  padding: 48px 20px;
  font-size: 14px;
  line-height: 1.7;
}
.empty .big { font-size: 40px; display: block; margin-bottom: 10px; }

/* ===== ローディング ===== */
.spinner {
  display: inline-block; width: 15px; height: 15px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-inline { color: var(--text-muted); font-size: 14px; display: flex; align-items: center; gap: 8px; padding: 8px 2px; }

/* ===== モーダル ===== */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
}
.modal {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 22px;
  width: 100%; max-width: 420px;
}
.modal-title { margin: 0 0 10px; font-size: 18px; }
.modal-desc { margin: 0 0 14px; font-size: 13px; color: var(--text-muted); line-height: 1.6; }
.modal-note { margin: 8px 0 0; font-size: 11px; color: var(--text-dim); line-height: 1.5; }
.modal-row { display: flex; gap: 10px; margin-top: 16px; }
.modal-row > * { flex: 1; }

/* ===== トースト ===== */
.toast {
  position: fixed;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  left: 50%; transform: translateX(-50%);
  background: var(--bg-active);
  color: var(--text);
  padding: 11px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 200;
  box-shadow: 0 6px 20px rgba(0,0,0,.4);
  border: 1px solid var(--border);
}

/* ===== ユーティリティ ===== */
.hidden { display: none !important; }
.mt8 { margin-top: 8px; } .mt12 { margin-top: 12px; } .mt16 { margin-top: 16px; }
.gap-actions { display: flex; gap: 10px; margin-bottom: 12px; }
.gap-actions > * { flex: 1; }
