:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #667085;
  --border: #e3e6ec;
  --accent: #0f7b6c;
  --accent-soft: #e3f3f0;
  --home: #226699;
  --home-soft: #e6eff7;
  --danger: #c0392b;
  --canvas-light: #ffffff;
  --canvas-dark: #15171c;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --panel: #181b21;
    --text: #e6e8ec;
    --muted: #9aa3b2;
    --border: #2a2f38;
    --accent: #35c2a8;
    --accent-soft: #17332e;
  --home: #7db4e0;
  --home-soft: #1a2633;
    --danger: #ff6b5b;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #181b21;
  --text: #e6e8ec;
  --muted: #9aa3b2;
  --border: #2a2f38;
  --accent: #35c2a8;
  --accent-soft: #17332e;
  --home: #7db4e0;
  --home-soft: #1a2633;
  --danger: #ff6b5b;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 15px;
  line-height: 1.55;
}
a { color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.wrap { max-width: 1280px; margin: 0 auto; padding: 0 16px; }

/* Header */
.site-header { border-bottom: 1px solid var(--border); background: var(--panel); }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-top: 14px; padding-bottom: 14px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.home-link {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 10px;
  color: var(--home); background: var(--home-soft); border: 1px solid transparent;
  transition: transform .15s ease, background-color .15s ease, border-color .15s ease;
}
.home-link:hover { transform: translateY(-1px); border-color: var(--home); }
.home-link:focus-visible { outline: 2px solid var(--home); outline-offset: 2px; }
.brand-divider { flex: none; width: 1px; height: 28px; background: var(--border); }
.brand-mark { font-size: 30px; color: var(--accent); line-height: 1; }
.brand h1 { font-size: 20px; margin: 0; letter-spacing: .3px; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

/* Layout */
.layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; padding-top: 16px; padding-bottom: 16px; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.library { padding: 0; }
.lib-top { padding: 18px 18px 0; }
.lib-scroll { padding: 0 18px 18px; }

/* 桌機：整頁固定一個螢幕高，左右兩欄各自獨立捲動，互不牽動 */
@media (min-width: 901px) {
  body { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
  .site-header { flex: none; }
  .header-inner { padding-top: 10px; padding-bottom: 10px; }
  .layout {
    flex: 1; min-height: 0; width: 100%;
    grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
  }
  .playground, .lib-scroll { overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable; }
  .library { display: flex; flex-direction: column; overflow: hidden; }
  .lib-top { flex: none; border-bottom: 1px solid var(--border); }
  .lib-scroll { flex: 1; min-height: 0; padding-top: 14px; }
  /* 讓「輸入框＋結構圖＋分子資訊＋按鈕」在一個畫面內就看得到 */
  .canvas-box { aspect-ratio: auto; height: clamp(260px, calc(100dvh - 420px), 640px); max-height: none; }
}

/* Input */
.field-label { display: block; font-weight: 600; font-size: 13px; color: var(--muted); margin-bottom: 6px; letter-spacing: .5px; }
.input-row { display: flex; gap: 8px; align-items: flex-start; }
#smilesInput {
  flex: 1; min-width: 0; resize: vertical; font-family: var(--mono); font-size: 15px;
  padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); word-break: break-all;
}
#smilesInput:focus, .search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#smilesInput.invalid { border-color: var(--danger); outline-color: var(--danger); }
.status { min-height: 1.4em; margin: 6px 0 10px; font-size: 13px; color: var(--muted); }
.status.error { color: var(--danger); }

.icon-btn {
  flex: none; width: 38px; height: 38px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font-size: 16px; cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Canvas */
.canvas-box {
  position: relative; aspect-ratio: 1 / 1; width: 100%; max-height: 62vh;
  border: 1px solid var(--border); border-radius: 10px; overflow: hidden;
  background: var(--canvas-light); transition: background .2s;
}
.canvas-box.mol-dark { background: var(--canvas-dark); }
#molSvg { width: 100%; height: 100%; display: block; }
.empty-hint {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: #98a2b3; font-size: 14px; text-align: center; padding: 16px; pointer-events: none;
}

/* Info */
.mol-info { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 12px 0; }
.mol-info > div { background: var(--bg); border-radius: 8px; padding: 8px 10px; min-width: 0; }
.mol-info dt { font-size: 12px; color: var(--muted); }
.mol-info dd { margin: 0; font-weight: 600; font-family: var(--mono); overflow-wrap: anywhere; }
@media (max-width: 480px) { .mol-info { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }

/* Buttons */
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn {
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  padding: 8px 12px; border-radius: 8px; font-size: 14px; cursor: pointer; font-family: inherit;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-ghost { margin-top: 12px; }

/* Detail */
.detail { margin-top: 16px; padding: 14px 16px; border-radius: 10px; background: var(--accent-soft); }
.detail h3 { margin: 6px 0 0; font-size: 18px; }
.detail p { margin: 6px 0; }
.inci { color: var(--muted); font-style: italic; margin-top: 0 !important; }
.aliases, .lookup { font-size: 13px; color: var(--muted); }
.note { font-size: 13px; border-left: 3px solid var(--accent); padding-left: 8px; }
.note:empty, .aliases:empty { display: none; }

/* Options */
.options { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 12px; }
.options summary { cursor: pointer; font-weight: 600; }
.opt-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; margin-top: 12px; }
@media (max-width: 480px) { .opt-grid { grid-template-columns: minmax(0, 1fr); } }
.opt-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.opt-grid label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.opt-grid select { padding: 6px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 14px; }
.opt-grid input[type="range"] { accent-color: var(--accent); width: 100%; }
.opt-grid output { font-family: var(--mono); color: var(--text); }

/* Library */
.lib-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.lib-head h2 { margin: 0 0 10px; font-size: 18px; }
.count { color: var(--muted); font-size: 13px; }
.search {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 15px; font-family: inherit;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.chip {
  --c: var(--accent);
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  padding: 4px 10px; border-radius: 999px; font-size: 13px; cursor: pointer; font-family: inherit;
}
.chip::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--c); margin-right: 6px; vertical-align: 1px; }
.chip[aria-pressed="true"] { border-color: var(--c); background: color-mix(in srgb, var(--c) 14%, transparent); }
.chip-static { cursor: default; font-size: 12px; background: var(--panel); }

.ing-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.ing-card {
  display: flex; flex-direction: column; width: 100%; text-align: left; cursor: pointer;
  border: 1px solid var(--border); border-radius: 10px; background: var(--panel); color: var(--text);
  padding: 8px; font-family: inherit; height: 100%;
}
.ing-card:hover { border-color: var(--accent); }
.ing-card[aria-current="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
.thumb { aspect-ratio: 1 / 1; width: 100%; border-radius: 6px; background: var(--canvas-light); overflow: hidden; }
.thumb svg { width: 100%; height: 100%; display: block; }
.ing-name { font-weight: 600; font-size: 14px; margin-top: 6px; line-height: 1.3; }
.ing-inci { font-size: 12px; color: var(--muted); line-height: 1.3; overflow-wrap: anywhere; }
.ing-cat { font-size: 11px; color: var(--muted); margin-top: auto; padding-top: 4px; }
.ing-cat::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--c); margin-right: 5px; }
.no-result { color: var(--muted); text-align: center; padding: 24px 0; }

.site-footer { color: var(--muted); font-size: 12px; margin-top: 20px; border-top: 1px solid var(--border); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: #1d2330; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }
