:root {
  --bg: #f6f5f2;
  --bg-2: #ecebe7;
  --panel: #ffffff;
  --panel-2: #f3f2ef;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: #e2e0db;
  --accent: #4f6bed;
  --accent-soft: rgba(79, 107, 237, 0.12);
  --danger: #d93434;
  --stage: #e7e6e2;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.14), 0 1px 3px rgba(0, 0, 0, 0.08);
  --radius: 14px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17181b; --bg-2: #202226; --panel: #222428; --panel-2: #2a2c31; --text: #f2f2f4; --muted: #9a9ba3;
    --line: #34363c; --accent: #7b91ff; --accent-soft: rgba(123, 145, 255, 0.16); --stage: #111214;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17181b; --bg-2: #202226; --panel: #222428; --panel-2: #2a2c31; --text: #f2f2f4; --muted: #9a9ba3;
  --line: #34363c; --accent: #7b91ff; --accent-soft: rgba(123, 145, 255, 0.16); --stage: #111214;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
body { position: fixed; inset: 0; }
#app { position: absolute; inset: 0; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; -webkit-user-select: none; user-select: none; touch-action: manipulation; }
button:disabled { opacity: 0.35; cursor: default; }
input, select, textarea { font: inherit; color: inherit; }
svg { display: block; }
[hidden] { display: none !important; }
.grow { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.row.center { justify-content: center; }

/* ---------- common controls ---------- */
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; display: inline-grid; place-items: center; flex: none;
  color: var(--text); transition: background .15s;
}
.icon-btn:hover { background: var(--bg-2); }
.icon-btn:active { background: var(--line); }
.icon-btn.sm { width: 32px; height: 32px; border-radius: 8px; font-size: 12px; font-weight: 600; }
.icon-btn.sm svg { width: 18px; height: 18px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 40px; padding: 0 16px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line);
  font-weight: 600; text-decoration: none; color: var(--text); white-space: nowrap;
}
.btn svg { width: 18px; height: 18px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary.danger { background: var(--danger); border-color: var(--danger); }
.btn.block { width: 100%; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line); font-size: 13px; font-weight: 600; white-space: nowrap; flex: none;
}
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip svg { width: 16px; height: 16px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.seg { display: inline-flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 2px; gap: 2px; flex: none; }
.seg button { height: 30px; padding: 0 12px; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.seg button.on { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.input { width: 100%; height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); padding: 0 12px; font-size: 16px; outline: none; }
.input:focus { border-color: var(--accent); }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--panel); box-shadow: 0 0 0 1px var(--line); flex: none; }
.swatch.on { box-shadow: 0 0 0 2px var(--accent); }
.swatch.sm { width: 22px; height: 22px; }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translate(-50%, 20px);
  background: rgba(30, 30, 32, 0.92); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 100; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- popovers & dialogs ---------- */
.pop-wrap { position: fixed; inset: 0; z-index: 50; }
.scrim { position: absolute; inset: 0; }
.popover {
  position: absolute; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 6px; max-height: calc(100vh - 24px); overflow: auto; min-width: 200px;
  animation: pop .14s ease-out;
}
.popover.wide { width: min(420px, calc(100vw - 16px)); padding: 14px; }
@keyframes pop { from { opacity: 0; transform: scale(.97); } }
.menu { display: flex; flex-direction: column; }
.menu-item { display: flex; align-items: center; gap: 10px; height: 42px; padding: 0 12px; border-radius: 8px; text-align: left; white-space: nowrap; }
.menu-item:hover { background: var(--bg-2); }
.menu-item.danger { color: var(--danger); }
.mi-icon { width: 20px; display: grid; place-items: center; color: var(--muted); }
.menu-item.danger .mi-icon { color: var(--danger); }
.mi-icon svg { width: 18px; height: 18px; }
.mi-check { margin-left: auto; color: var(--accent); }
.mi-check svg { width: 18px; height: 18px; }
.menu-sep { height: 1px; background: var(--line); margin: 4px 6px; }

.dialog-wrap { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.35); display: grid; place-items: center; padding: 16px; animation: fade .15s; }
@keyframes fade { from { opacity: 0; } }
.dialog { background: var(--panel); border-radius: 18px; padding: 20px; width: min(400px, 100%); box-shadow: var(--shadow); max-height: calc(100vh - 32px); overflow: auto; }
.dialog h3 { margin: 0 0 12px; font-size: 18px; }
.dialog h4 { margin: 18px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.dialog p { margin: 6px 0; }
.dialog.settings { width: min(480px, 100%); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.toggle small { display: block; color: var(--muted); font-size: 12px; }
.toggle input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.progress { height: 8px; background: var(--bg-2); border-radius: 99px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

/* ---------- library ---------- */
.library { position: absolute; inset: 0; display: flex; }
.lib-side {
  width: 250px; flex: none; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: calc(14px + var(--safe-t)) 10px calc(12px + var(--safe-b)) calc(10px + var(--safe-l)); gap: 2px; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 19px; padding: 4px 10px 16px; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }
.side-h { display: flex; align-items: center; justify-content: space-between; padding: 16px 6px 4px 12px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }
.subj { display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 0 8px 0 12px; border-radius: 10px; text-align: left; width: 100%; }
.subj:hover { background: var(--bg-2); }
.subj.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.subj .si { color: var(--muted); }
.subj.on .si { color: var(--accent); }
.subj .si svg { width: 18px; height: 18px; }
.subj .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.subj .count { font-size: 12px; color: var(--muted); }
.subj-more { opacity: .5; padding: 4px; }
.subj-more svg { width: 16px; height: 16px; }
.lib-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.lib-head { display: flex; align-items: center; gap: 8px; padding: calc(12px + var(--safe-t)) calc(20px + var(--safe-r)) 10px 24px; }
.lib-head h1 { font-size: 26px; margin: 0; letter-spacing: -.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-wrap { display: flex; align-items: center; gap: 6px; height: 40px; padding: 0 12px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); width: min(260px, 40vw); color: var(--muted); }
.search-wrap svg { width: 18px; height: 18px; flex: none; }
.search { border: 0; outline: 0; background: none; width: 100%; font-size: 16px; color: var(--text); }
.new-btn svg { width: 18px; height: 18px; }
.lib-chips { display: none; gap: 6px; padding: 0 16px 10px; overflow-x: auto; scrollbar-width: none; }
.lib-chips::-webkit-scrollbar { display: none; }
.grid-wrap { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 8px calc(20px + var(--safe-r)) calc(30px + var(--safe-b)) 24px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 22px 18px; }
.card { display: flex; flex-direction: column; gap: 8px; min-width: 0; -webkit-touch-callout: none; }
.card-open { display: block; width: 100%; }
.thumb { aspect-ratio: 3 / 4; border-radius: 10px; overflow: hidden; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.08), 0 4px 14px rgba(0,0,0,.07); border: 1px solid var(--line); transition: transform .15s; }
.card-open:hover .thumb { transform: translateY(-2px); }
.thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; display: block; -webkit-user-drag: none; }
.thumb-blank { width: 100%; height: 100%; background: repeating-linear-gradient(#fff 0 22px, #dfe6f3 22px 23px); }
.card-meta { display: flex; align-items: flex-start; gap: 4px; }
.card-text { flex: 1; min-width: 0; }
.card-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-sub { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.empty { grid-column: 1 / -1; text-align: center; padding: 70px 20px; }
.empty-art { width: 64px; height: 64px; margin: 0 auto 12px; border-radius: 18px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.empty-art svg { width: 32px; height: 32px; }
.empty h2 { margin: 8px 0 4px; }
.phone-only { display: none; }

@media (max-width: 720px) {
  .lib-side { display: none; }
  .lib-chips { display: flex; }
  .phone-only { display: inline-grid; }
  .lib-head { padding: calc(10px + var(--safe-t)) 12px 10px; }
  .lib-head h1 { font-size: 22px; }
  .search-wrap { width: 40px; padding: 0; justify-content: center; position: relative; }
  .search-wrap:focus-within { width: 46vw; padding: 0 10px; justify-content: flex-start; }
  .search-wrap .search { width: 0; }
  .search-wrap:focus-within .search { width: 100%; }
  .grid-wrap { padding: 4px 12px calc(30px + var(--safe-b)); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 16px 12px; }
  .hide-xs { display: none; }
  .new-btn { padding: 0 12px; }
}

/* ---------- editor ---------- */
.editor { position: absolute; inset: 0; display: flex; flex-direction: column; background: var(--stage); }
.ed-top {
  display: flex; align-items: center; gap: 2px; background: var(--panel);
  padding: calc(4px + var(--safe-t)) calc(8px + var(--safe-r)) 2px calc(6px + var(--safe-l)); flex: none;
}
.title-input { flex: 0 1 360px; min-width: 60px; border: 0; outline: 0; background: none; font-weight: 700; font-size: 17px; padding: 6px 8px; border-radius: 8px; text-overflow: ellipsis; }
.title-input:focus { background: var(--panel-2); }
.rec-btn { width: auto; min-width: 40px; padding: 0 8px; gap: 4px; display: inline-flex; }
.rec-btn.recording { color: #fff; background: var(--danger); animation: pulse 1.6s infinite; }
.rec-time { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
@keyframes pulse { 50% { opacity: .75; } }

.ed-toolbar {
  display: flex; align-items: center; gap: 6px; background: var(--panel); border-bottom: 1px solid var(--line);
  padding: 4px calc(10px + var(--safe-r)) 6px calc(10px + var(--safe-l)); overflow-x: auto; scrollbar-width: none; flex: none;
}
.ed-toolbar::-webkit-scrollbar { display: none; }
.tb-group { display: flex; align-items: center; gap: 2px; flex: none; }
.tb-group.opts { gap: 6px; }
.tb-sep { width: 1px; height: 26px; background: var(--line); margin: 0 4px; flex: none; }
.tb-sep.sm { height: 20px; margin: 0 2px; }
.tool-btn { width: 44px; height: 40px; border-radius: 10px; display: grid; place-items: center; color: var(--muted); flex: none; }
.tool-btn:hover { background: var(--bg-2); }
.tool-btn.on { color: var(--accent); background: var(--accent-soft); }
.size-btn { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; flex: none; }
.size-btn.on { background: var(--bg-2); box-shadow: inset 0 0 0 1.5px var(--accent); }
.size-dot { border-radius: 50%; display: block; }
.size-ring { border-radius: 50%; border: 1.5px solid var(--text); display: block; }
.size-txt { font-weight: 700; line-height: 1; }
.hint { font-size: 13px; color: var(--muted); white-space: nowrap; padding: 0 4px; }
.hidden-color { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

.ed-body { flex: 1; display: flex; min-height: 0; }
.stage {
  flex: 1; position: relative; overflow: hidden; min-width: 0; background: var(--stage);
  touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.stage canvas { position: absolute; left: 0; top: 0; touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.cv-live { z-index: 2; }
.text-edit {
  position: absolute; z-index: 3; border: 1.5px dashed var(--accent); border-radius: 4px; background: rgba(255,255,255,0.0);
  outline: 0; resize: none; overflow: hidden; padding: 0; margin: -2px 0 0 -2px; line-height: 1.3; min-height: 1.3em;
  -webkit-user-select: text; user-select: text; touch-action: auto;
}
.sel-menu {
  position: absolute; left: 0; top: 0; z-index: 4; display: flex; align-items: center; gap: 2px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 4px;
}
.sel-colors { display: flex; gap: 4px; padding: 0 6px; }
.sel-btn { width: 38px; height: 36px; border-radius: 8px; display: grid; place-items: center; }
.sel-btn:hover { background: var(--bg-2); }
.sel-btn svg { width: 19px; height: 19px; }
.sel-btn.danger { color: var(--danger); }
.page-ind {
  position: absolute; z-index: 4; left: 50%; bottom: calc(16px + var(--safe-b)); transform: translateX(-50%);
  background: rgba(30,30,32,.8); color: #fff; font-size: 13px; font-weight: 600; padding: 5px 12px; border-radius: 99px;
  opacity: 0; transition: opacity .25s; pointer-events: none; font-variant-numeric: tabular-nums;
}
.page-ind.show { opacity: 1; }
.fab {
  position: absolute; z-index: 4; right: calc(16px + var(--safe-r)); bottom: calc(16px + var(--safe-b)); width: 46px; height: 46px; border-radius: 50%;
  background: var(--panel); color: var(--text); box-shadow: var(--shadow); display: grid; place-items: center; border: 1px solid var(--line);
}

.pages-panel { width: 230px; flex: none; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; padding: 10px 10px calc(10px + var(--safe-b)); gap: 8px; overflow: hidden; }
.pp-head { display: flex; align-items: center; padding-left: 4px; }
.pp-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; padding: 4px; }
.pp-item { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.pp-open { position: relative; width: 150px; }
.pp-thumb { width: 150px; border-radius: 6px; overflow: hidden; background: #fff; border: 1px solid var(--line); box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.pp-thumb canvas { width: 100%; height: 100%; display: block; }
.pp-thumb.current { outline: 3px solid var(--accent); outline-offset: 1px; }
.pp-num { position: absolute; left: 6px; bottom: 6px; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 99px; }
.pp-actions { display: flex; gap: 2px; }

.audio-bar {
  display: flex; align-items: center; gap: 6px; background: var(--panel); border-top: 1px solid var(--line);
  padding: 6px calc(12px + var(--safe-r)) calc(6px + var(--safe-b)) calc(12px + var(--safe-l)); flex: none; overflow-x: auto;
}
.ab-icon { color: var(--danger); }
.rec-select { height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); padding: 0 8px; max-width: 200px; font-size: 14px; }
.seek { flex: 1; min-width: 100px; accent-color: var(--accent); }
.time { font-size: 13px; font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }

.paper-pop h4 { margin: 14px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.tpl-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.tpl { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.tpl canvas { width: 100%; height: auto; border-radius: 6px; border: 1px solid var(--line); }
.tpl.on canvas { outline: 2.5px solid var(--accent); outline-offset: 1px; }
.tpl.on { color: var(--accent); font-weight: 600; }

@media (max-width: 720px) {
  .pages-panel { position: absolute; right: 0; top: 0; bottom: 0; z-index: 10; box-shadow: var(--shadow); }
  .ed-body { position: relative; }
  .title-input { font-size: 16px; }
  .ed-top .icon-btn { width: 36px; }
  .tool-btn { width: 40px; }
  .audio-bar .rec-select { max-width: 110px; }
}
@media print { .ed-top, .ed-toolbar, .audio-bar { display: none; } }
