:root {
  --bg: #0c0f14;
  --bg-elevated: #141a22;
  --bg-soft: #1a2230;
  --border: #2a3544;
  --text: #e8eef6;
  --muted: #8b9bb0;
  --accent: #5b9fd4;
  --accent-2: #7c6af5;
  --ok: #3ecf8e;
  --warn: #e6b84d;
  --danger: #f07178;
  --record: #6cb6ff;
  --radius: 12px;
  --font: "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --mono: "Cascadia Code", "Consolas", "SF Mono", monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font);
  background: radial-gradient(1200px 600px at 10% -10%, #1a2740 0%, transparent 55%),
              radial-gradient(900px 500px at 100% 0%, #1f1838 0%, transparent 50%),
              var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app {
  max-width: min(1680px, 100%);
  margin: 0 auto;
  padding: 16px 20px 32px;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 4px 4px;
}
.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 650;
  letter-spacing: 0.02em;
}
.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.badges { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.badge {
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--muted);
}
.badge.ok { color: var(--ok); border-color: #2a5a45; }
.badge.warn { color: var(--warn); border-color: #5a4a20; }

.layout {
  display: grid;
  /* 左：操作；右：大閱讀區 */
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr);
  gap: 16px;
  flex: 1;
  min-height: calc(100vh - 110px);
  align-items: stretch;
}
@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
    min-height: auto;
  }
}

.panel {
  background: color-mix(in srgb, var(--bg-elevated) 92%, transparent);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}
.panel-input {
  max-height: calc(100vh - 110px);
  overflow: auto;
}
.panel-result {
  min-height: calc(100vh - 110px);
}
.panel-input textarea {
  min-height: 280px;
  max-height: none;
  height: min(42vh, 420px);
  flex: 1;
}
.panel-input .input-block {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.sidebar-section {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}
.title-line {
  font-size: 1.15rem;
  font-weight: 650;
  margin: 0;
  line-height: 1.4;
}
.panel h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}
.panel h2 span.step {
  display: inline-flex;
  width: 22px;
  height: 22px;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  font-size: 0.75rem;
  font-weight: 700;
}

label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 4px;
}
textarea, input[type="text"], input[type="password"], select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font: inherit;
  outline: none;
}
textarea {
  min-height: 160px;
  resize: vertical;
  font-family: var(--mono);
  font-size: 0.85rem;
  line-height: 1.45;
}
textarea:focus, input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
@media (max-width: 560px) {
  .row { grid-template-columns: 1fr; }
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}
.controls .grow { flex: 1; min-width: 120px; }

button, .btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 14px;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:hover, .btn:hover { background: #243044; border-color: #3d4d63; }
button:active { transform: translateY(1px); }
button.primary {
  background: linear-gradient(135deg, #3d7eb0, #5b6ad4);
  border-color: transparent;
  font-weight: 600;
}
button.primary:hover { filter: brightness(1.08); }
button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}
button.ghost { background: transparent; }
button.danger {
  background: #3a1c1c;
  border-color: #7a3030;
  color: #f0a0a0;
  font-weight: 600;
}
button.danger:hover {
  background: #4a2222;
  border-color: #a04040;
  filter: none;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.85rem;
  user-select: none;
}
.checkbox input { width: auto; }

.settings {
  border-top: 1px dashed var(--border);
  padding-top: 12px;
  display: none;
  flex-direction: column;
  gap: 10px;
}
.settings.open { display: flex; }

.hint {
  font-size: 0.78rem;
  color: var(--muted);
}

.status-line {
  min-height: 1.2em;
  font-size: 0.85rem;
  color: var(--muted);
}
.status-line.error { color: var(--danger); }
.status-line.ok { color: var(--ok); }

.decision {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 8px;
  font-weight: 650;
  font-size: 0.9rem;
}
.decision.approve { background: #143528; color: var(--ok); }
.decision.record { background: #152636; color: var(--record); }
.decision.review_required { background: #3a2420; color: var(--danger); }

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0;
}
.tab {
  padding: 8px 12px;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
  border-radius: 0;
}
.tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}
.tab-panel { display: none; flex: 1; min-height: 0; flex-direction: column; }
.tab-panel.active { display: flex; }
.sticky-tabs {
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 2;
  background: color-mix(in srgb, var(--bg-elevated) 96%, transparent);
  margin-bottom: 0;
}
.result-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: 0;
}
.result-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.result-actions {
  justify-content: flex-end;
}

/* 大閱讀窗：可睇全文 */
.article-panel {
  flex: 1;
  min-height: 0;
}
.article-scroll {
  flex: 1;
  min-height: min(72vh, 900px);
  height: calc(100vh - 240px);
  max-height: none;
  overflow: auto;
  padding: 8px 4px 24px;
  scroll-behavior: smooth;
}
@media (max-width: 960px) {
  .article-scroll {
    height: auto;
    min-height: 60vh;
    max-height: none;
  }
  .panel-result { min-height: auto; }
  .panel-input { max-height: none; }
}

/* Markdown 正文（正常閱讀版面） */
.md-body {
  font-size: 1.02rem;
  line-height: 1.75;
  color: #e4eaf2;
  max-width: 52rem;
  margin: 0 auto;
  padding: 20px 28px 40px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  word-break: break-word;
}
.md-body > *:first-child { margin-top: 0; }
.md-body h1, .md-body h2, .md-body h3, .md-body h4 {
  color: var(--text);
  line-height: 1.35;
  margin: 1.35em 0 0.55em;
  font-weight: 650;
}
.md-body h1 { font-size: 1.55rem; border-bottom: 1px solid var(--border); padding-bottom: 0.35em; }
.md-body h2 { font-size: 1.28rem; }
.md-body h3 { font-size: 1.1rem; }
.md-body p { margin: 0.75em 0; }
.md-body ul, .md-body ol { margin: 0.6em 0; padding-left: 1.4em; }
.md-body li { margin: 0.3em 0; }
.md-body blockquote {
  margin: 0.9em 0;
  padding: 0.4em 0 0.4em 1em;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}
.md-body code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--bg-soft);
  padding: 0.12em 0.35em;
  border-radius: 4px;
}
.md-body pre {
  font-family: var(--mono);
  font-size: 0.86rem;
  background: #0a0d12;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  overflow: auto;
  line-height: 1.5;
}
.md-body pre code {
  background: transparent;
  padding: 0;
}
.md-body a { color: var(--accent); }
.md-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5em 0;
}
.md-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
  font-size: 0.95rem;
}
.md-body th, .md-body td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
}
.md-body th { background: var(--bg-soft); }

.log-block {
  max-height: none;
  white-space: pre-wrap;
  font-size: 0.78rem;
}

.card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin-top: 10px;
}
.card h3 {
  margin: 0 0 8px;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}
.score-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 8px 0;
}
.score-chip {
  font-size: 0.8rem;
  padding: 4px 8px;
  border-radius: 6px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
}
pre, .mono {
  font-family: var(--mono);
  font-size: 0.8rem;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
}
.report {
  font-size: 0.88rem;
  color: #d5dde8;
}
.report h1, .report h2, .report h3 { color: var(--text); }
ul.compact { margin: 0; padding-left: 1.2em; }
ul.compact li { margin: 2px 0; }

.history {
  max-height: 180px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 0;
}
.tag-chip {
  display: inline-flex;
  align-items: center;
  font-size: 0.75rem;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--accent);
  cursor: pointer;
  user-select: none;
}
.tag-chip:hover, .tag-chip.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 18%, var(--bg));
  color: var(--text);
}
.tag-chip.kw {
  color: var(--muted);
  border-style: dashed;
}
.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 6px 0 2px;
}
.history-item {
  text-align: left;
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
}
.history-item:hover { border-color: var(--accent); }
.history-item .meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--muted);
}
.history-item .sum {
  font-size: 0.82rem;
  margin-top: 4px;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.footer {
  color: var(--muted);
  font-size: 0.75rem;
  text-align: center;
  padding-top: 4px;
}
.footer a { color: var(--accent); text-decoration: none; }

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid #ffffff55;
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty {
  color: var(--muted);
  font-size: 0.9rem;
  padding: 24px 8px;
  text-align: center;
}

.steps-timeline {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
  max-height: none;
  overflow: visible;
}
.step-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--bg);
  font-size: 0.8rem;
}
.step-row.status-error { border-color: #7a3030; background: #2a1515; }
.step-row.status-warn { border-color: #6a5a20; }
.step-row.status-skip { opacity: 0.75; }
.step-row .step-head {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: space-between;
  color: var(--muted);
}
.step-row .step-name {
  color: var(--text);
  font-weight: 600;
}
.step-row .step-detail {
  margin-top: 4px;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: #b8c4d4;
  white-space: pre-wrap;
  word-break: break-word;
}
