:root {
  --bg: #f6f1e7; --fg: #2b2620; --muted: #7a6f60; --panel: #efe8d9; --line: #ddd2bc;
  --accent: #8a4b1c; --link: #8a4b1c; --hl: #ffe08a; --input: #fbf8f1;
}
[data-theme="light"] { --bg:#ffffff; --fg:#1d1d1f; --muted:#6e6e73; --panel:#f5f5f7; --line:#e0e0e4; --accent:#0a66c2; --link:#0a66c2; --hl:#fff1a8; --input:#fff; }
[data-theme="dark"]  { --bg:#16171a; --fg:#d9d6cf; --muted:#8d8a83; --panel:#1f2024; --line:#2e2f35; --accent:#e0a458; --link:#e0a458; --hl:#6b5a1f; --input:#26272c; }

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column; background: var(--bg); color: var(--fg);
  font: 13px/1.4 -apple-system, "Segoe UI", system-ui, sans-serif; overflow: hidden;
}
button { font: inherit; color: inherit; background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 4px 9px; cursor: pointer; }
button:hover { background: var(--line); }
input { font: inherit; color: var(--fg); background: var(--input); border: 1px solid var(--line); border-radius: 6px; padding: 5px 9px; outline: none; }
input:focus { border-color: var(--accent); }

#topbar {
  display: flex; align-items: center; gap: 4px; height: 44px; padding: 0 10px;
  background: var(--panel); border-bottom: 1px solid var(--line); -webkit-app-region: drag;
}
body.mac #topbar { padding-left: 84px; }
#topbar > * { -webkit-app-region: no-drag; }
#topbar button { font-size: 16px; line-height: 1; }
#btn-bookmark.on { color: var(--accent); }
#doc-title { flex: 1; min-width: 0; text-align: center; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 12px; -webkit-app-region: drag; }
#search-input { width: 240px; }

#settings {
  position: fixed; top: 48px; right: 10px; z-index: 20; width: 290px; padding: 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 30px #0003;
  display: flex; flex-direction: column; gap: 12px;
}
#settings[hidden], #findbar[hidden] { display: none; }
#settings label { display: flex; flex-direction: column; gap: 5px; color: var(--muted); font-size: 12px; }
#settings label.row { flex-direction: row; align-items: center; gap: 8px; }
#settings input[type=range] { padding: 0; accent-color: var(--accent); }
.seg { display: flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.seg button { flex: 1; border-radius: 0; font-size: 12px; padding: 5px 4px; }
.seg button.on { background: var(--accent); color: var(--bg); }

#findbar {
  position: fixed; top: 48px; left: 50%; transform: translateX(-50%); z-index: 20; display: flex; gap: 4px; align-items: center;
  padding: 6px 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 30px #0003;
}
#find-count { color: var(--muted); min-width: 56px; text-align: center; font-size: 12px; }

#layout { flex: 1; display: flex; min-height: 0; }
#sidebar { width: 340px; flex: none; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); }
body.no-sidebar #sidebar { display: none; }
#tabs { display: flex; border-bottom: 1px solid var(--line); }
#tabs button { flex: 1; border-radius: 0; padding: 9px 4px; color: var(--muted); border-bottom: 2px solid transparent; }
#tabs button.active { color: var(--fg); border-bottom-color: var(--accent); }
.pane { display: none; flex: 1; min-height: 0; overflow-y: auto; padding: 10px; }
.pane.active { display: block; }
.filter { width: 100%; margin-bottom: 8px; }
h4 { margin: 12px 4px 6px; color: var(--muted); font-size: 11px; letter-spacing: .06em; text-transform: uppercase; }

details { margin-bottom: 4px; }
summary { cursor: pointer; padding: 6px 4px; font-weight: 600; }
.item { display: block; padding: 5px 8px; border-radius: 6px; color: var(--fg); text-decoration: none; cursor: pointer; line-height: 1.35; }
.item:hover { background: var(--line); }
.item.current { background: var(--line); color: var(--accent); }
.item small { display: block; color: var(--muted); font-size: 11px; }
.item.lvl1 { padding-left: 22px; } .item.lvl2 { padding-left: 36px; font-size: 12px; }
.group-title { margin: 10px 8px 2px; font-weight: 600; color: var(--accent); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
.item .snip { color: var(--muted); font-size: 12px; margin-top: 2px; }
.item .row { display: flex; justify-content: space-between; gap: 8px; }
.item .x { color: var(--muted); }
.empty { color: var(--muted); padding: 8px; }
.wordgrid { display: flex; flex-wrap: wrap; gap: 3px; padding: 4px; }
.wordgrid a { padding: 2px 7px; border-radius: 5px; background: var(--bg); border: 1px solid var(--line); color: var(--fg); text-decoration: none; font-size: 12px; cursor: pointer; }
.wordgrid a:hover { border-color: var(--accent); }

#reader { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#doctabs { display: flex; gap: 2px; padding: 4px 6px 0; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; flex: none; }
#doctabs[hidden] { display: none; }
.dtab { display: flex; align-items: center; gap: 6px; max-width: 200px; padding: 5px 8px; border-radius: 6px 6px 0 0; font-size: 12px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.dtab span { overflow: hidden; text-overflow: ellipsis; }
.dtab.active { background: var(--bg); color: var(--fg); }
.dtab b { font-weight: 400; opacity: .6; } .dtab b:hover { opacity: 1; }
.item.visited { opacity: .6; }
#reader iframe { width: 100%; flex: 1; min-height: 0; border: 0; background: var(--bg); display: block; }

#results { flex: 1; min-height: 0; overflow-y: auto; background: var(--bg); padding: 16px max(24px, calc((100% - 860px) / 2)); }
#results[hidden] { display: none; }
#results .item { display: block; padding: 8px 10px; border-radius: 6px; font-size: 14px; }
#results .snip { font-size: 13px; }
#results #search-status { color: var(--muted); margin-bottom: 8px; }
.rtab span { font-weight: 600; }

/* web */
#scrim { display: none; }
@media (max-width: 760px) {
  body { font-size: 15px; }
  #topbar { padding: 0 6px; gap: 2px; height: 48px; }
  #topbar button { min-width: 38px; min-height: 38px; padding: 0 4px; font-size: 18px; }
  #btn-forward, #btn-home, #btn-find { display: none; }
  #doc-title { display: none; }
  #search-form { flex: 1; min-width: 0; }
  #search-input { width: 100%; font-size: 16px; padding: 8px 10px; }
  input, .filter { font-size: 16px; }
  #sidebar { position: absolute; z-index: 30; top: 0; bottom: 0; left: 0; width: min(88vw, 360px); box-shadow: 4px 0 24px rgba(0,0,0,.3); }
  body.no-sidebar #sidebar { display: none; }
  #scrim { position: absolute; z-index: 25; inset: 0; background: rgba(0,0,0,.35); display: block; }
  body.no-sidebar #scrim { display: none; }
  .item { padding: 9px 8px; }
  .item.visited { opacity: .6; }
  #tabs button { padding: 12px 4px; }
  #settings { left: 10px; right: 10px; width: auto; max-height: calc(100vh - 64px); overflow-y: auto; }
  #findbar { left: 10px; right: 10px; transform: none; }
  #find-input { flex: 1; min-width: 0; font-size: 16px; }
  #results { padding: 12px 14px; }
  .dtab { max-width: 140px; }
}
#layout { position: relative; }

/* web: feedback when "Browse the library" is clicked */
#sidebar.flash { box-shadow: inset 0 0 0 2px #b98a3a; transition: box-shadow .2s; }
