/* Nib: a dense, neutral-grey studio interface. The paper is the only bright thing on screen. */
:root {
  --bar: #2a2a2a; --bg: #262626; --panel: #323232; --panel-2: #3a3a3a; --field: #232323; --hover: #404040;
  --line: #1c1c1c; --line-soft: #444; --text: #e3e3e3; --dim: #9b9b9b; --accent: #3d8fd6; --accent-soft: rgba(61,143,214,.22);
  --danger: #e5484d; --canvas-bg: #4b4b4b; --shadow: 0 8px 28px rgba(0,0,0,.45);
  --tb-w: 44px; --left-w: 228px; --right-w: 270px; --bar-h: 38px;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bar: #dcdcdc; --bg: #e6e6e6; --panel: #f0f0f0; --panel-2: #e2e2e2; --field: #ffffff; --hover: #e4e9ef;
  --line: #bdbdbd; --line-soft: #d2d2d2; --text: #1f1f1f; --dim: #666; --accent: #2a78c2; --accent-soft: rgba(42,120,194,.16);
  --canvas-bg: #a4a4a4; --shadow: 0 8px 28px rgba(0,0,0,.18);
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none;
  font: 12px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:disabled { opacity: .35; cursor: default; }
input, select, textarea { font: inherit; color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
[hidden] { display: none !important; }
.spacer { flex: 1; }

#app { display: grid; grid-template-rows: var(--bar-h) 1fr; height: 100dvh; padding-top: env(safe-area-inset-top); }

/* ---- top bar ---- */
#bar { display: flex; align-items: center; gap: 4px; padding: 0 8px; background: var(--bar); border-bottom: 1px solid var(--line); min-width: 0; }
.logo { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 14px; letter-spacing: .02em; padding: 4px 8px 4px 4px; border-radius: 4px; }
.logo:hover { background: var(--hover); }
#menus { display: flex; }
.menu-btn { padding: 5px 9px; border-radius: 4px; }
.menu-btn:hover, .menu-btn.on { background: var(--hover); }
#doc-title { margin-left: 10px; color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
#save-state { color: var(--dim); font-size: 11px; margin-right: 6px; white-space: nowrap; }
#save-state[data-s="error"] { color: var(--danger); }
.ib { display: inline-flex; align-items: center; justify-content: center; gap: 4px; min-width: 28px; height: 28px; padding: 0 5px; border-radius: 4px; color: var(--text); position: relative; }
.ib:hover:not(:disabled) { background: var(--hover); }
.ib i { font-style: normal; font-size: 10px; font-weight: 700; position: absolute; right: 2px; bottom: 1px; }
.ib.danger:hover { color: var(--danger); }
.narrow-only { display: none; }

.menu-pop { position: fixed; z-index: 50; min-width: 250px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; box-shadow: var(--shadow); padding: 4px; }
.menu-item { display: grid; grid-template-columns: 16px 1fr auto; gap: 6px; width: 100%; text-align: left; padding: 5px 8px; border-radius: 4px; }
.menu-item:hover:not(:disabled) { background: var(--accent); color: #fff; }
.mi-key { color: var(--dim); font-size: 11px; }
.menu-item:hover .mi-key { color: #fff; }
.menu-sep { height: 1px; background: var(--line-soft); margin: 4px 2px; }

/* ---- layout ---- */
#main { display: grid; grid-template-columns: var(--tb-w) var(--left-w) 1fr var(--right-w); min-height: 0; }
body.no-panels #main { grid-template-columns: var(--tb-w) 0 1fr 0; }
body.no-panels #left, body.no-panels #right { display: none; }
#toolbar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; padding: 6px 0; gap: 2px; overflow-y: auto; }
.tool { width: 36px; height: 34px; border-radius: 5px; display: grid; place-items: center; color: var(--text); }
.tool:hover { background: var(--hover); }
.tool.on { background: var(--accent-soft); color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tb-sep { width: 24px; height: 1px; background: var(--line-soft); margin: 4px 0; }
#left, #right { background: var(--panel); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
#left { border-right: 1px solid var(--line); }
#right { border-left: 1px solid var(--line); }
.pnl { display: flex; flex-direction: column; border-bottom: 1px solid var(--line); min-height: 0; }
.pnl.grow { flex: 1; }
.pnl > header { height: 24px; flex: none; display: flex; align-items: center; padding: 0 8px; background: var(--panel-2); border-bottom: 1px solid var(--line); font-weight: 600; font-size: 11px; color: var(--dim); text-transform: uppercase; letter-spacing: .05em; }
#sub-list { display: flex; flex-direction: column; padding: 4px; gap: 1px; max-height: 32vh; overflow-y: auto; }
.sub { display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 4px; text-align: left; }
.sub:hover { background: var(--hover); }
.sub.on { background: var(--accent); color: #fff; }
.sub-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: .55; flex: none; }
#props { padding: 8px; overflow-y: auto; display: flex; flex-direction: column; gap: 7px; }

/* ---- controls ---- */
.ctl { display: grid; grid-template-columns: 82px 1fr; align-items: center; gap: 6px; }
.ctl.slider { grid-template-columns: 82px 1fr 46px; }
.ctl .lbl { color: var(--dim); font-size: 11px; }
.ctl .val { text-align: right; font-variant-numeric: tabular-nums; font-size: 11px; }
.ctl.check { grid-template-columns: 16px 1fr; cursor: pointer; }
.ctl.stack, .ctl.area { grid-template-columns: 1fr; gap: 4px; }
.ctl.color { grid-template-columns: auto 30px; gap: 6px; }
.ctl-row { display: flex; flex-wrap: wrap; gap: 10px; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 18px; background: transparent; margin: 0; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--field); border: 1px solid var(--line); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 12px; height: 12px; margin-top: -5px; border-radius: 50%; background: var(--text); border: 2px solid var(--accent); }
input[type=range]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--field); border: 1px solid var(--line); }
input[type=range]::-moz-range-thumb { width: 10px; height: 10px; border-radius: 50%; background: var(--text); border: 2px solid var(--accent); }
input[type=checkbox] { accent-color: var(--accent); margin: 0; }
input[type=color] { width: 30px; height: 22px; border: 1px solid var(--line); border-radius: 3px; background: none; padding: 1px; cursor: pointer; }
select, .field, textarea { background: var(--field); border: 1px solid var(--line); border-radius: 4px; padding: 4px 6px; min-width: 0; width: 100%; }
textarea { resize: vertical; font-size: 13px; -webkit-user-select: text; user-select: text; }
.field { -webkit-user-select: text; user-select: text; }
.mono { font-family: ui-monospace, Menlo, monospace; font-size: 11px; }
.btn { background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; padding: 5px 10px; white-space: nowrap; display: inline-flex; align-items: center; justify-content: center; gap: 4px; }
.btn:hover:not(:disabled) { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); background: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 3px 10px; margin: 0 4px; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }
.hint { color: var(--dim); font-size: 11px; margin: 2px 0; line-height: 1.45; }
.grp { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.grp-title { font-size: 11px; color: var(--dim); font-weight: 600; }
.tpl-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.tpl-grid .btn { white-space: normal; padding: 6px 4px; font-size: 11px; }

/* ---- viewport ---- */
#viewport { position: relative; background: var(--canvas-bg); overflow: hidden; min-width: 0; touch-action: none; }
#view { position: absolute; inset: 0; touch-action: none; outline: none; }
#status { position: absolute; left: 8px; bottom: 8px; display: flex; align-items: center; gap: 6px; background: rgba(30,30,30,.82); color: #ddd; border-radius: 5px; padding: 3px 8px; font-size: 11px; pointer-events: auto; max-width: calc(100% - 16px); white-space: nowrap; overflow: hidden; }
#status button { color: #fff; padding: 1px 4px; border-radius: 3px; font-variant-numeric: tabular-nums; }
#status button:hover { background: rgba(255,255,255,.12); }
#status .dot { opacity: .5; }

.text-pop { position: absolute; z-index: 20; width: 280px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; box-shadow: var(--shadow); padding: 6px; display: flex; flex-direction: column; gap: 6px; }
.text-pop textarea { min-height: 70px; font-size: 15px; }
.tp-row { display: flex; align-items: center; justify-content: space-between; }

/* ---- color ---- */
.color-top { display: flex; align-items: center; gap: 6px; padding: 8px 8px 6px; }
.chips { position: relative; width: 38px; height: 30px; flex: none; }
.chip { position: absolute; width: 24px; height: 20px; border: 2px solid var(--panel); outline: 1px solid var(--line); border-radius: 3px; }
#c-main { left: 0; top: 0; z-index: 1; }
#c-sub { left: 13px; top: 10px; }
#c-hex { width: 84px; flex: none; margin-left: auto; }
#c-sv { display: block; width: calc(100% - 16px); height: 120px; margin: 0 8px; border-radius: 3px; cursor: crosshair; touch-action: none; }
#c-hue { display: block; width: calc(100% - 16px); height: 14px; margin: 6px 8px; border-radius: 3px; cursor: ew-resize; touch-action: none; }
#c-swatches { display: grid; grid-template-columns: repeat(14, 1fr); gap: 2px; padding: 0 8px 8px; }
.sw { aspect-ratio: 1; border-radius: 2px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.25); }
.sw:hover { transform: scale(1.15); }

/* ---- layers ---- */
#layer-props { padding: 6px 8px; display: flex; flex-direction: column; gap: 5px; border-bottom: 1px solid var(--line-soft); }
.lp-row { display: flex; gap: 4px; align-items: center; }
.lp-row .ctl { flex: 1; grid-template-columns: 1fr; }
.tog { width: 26px; height: 24px; border-radius: 4px; display: grid; place-items: center; color: var(--dim); border: 1px solid var(--line); background: var(--field); }
.tog.on { color: #fff; background: var(--accent); border-color: var(--accent); }
.tone-box { display: flex; flex-direction: column; gap: 5px; padding-top: 4px; border-top: 1px dashed var(--line-soft); }
#layer-list { list-style: none; margin: 0; padding: 2px 0; overflow-y: auto; flex: 1; min-height: 60px; }
.layer { display: flex; align-items: center; gap: 4px; height: 30px; padding: 0 6px 0 2px; border-bottom: 1px solid var(--line); cursor: pointer; }
.layer:hover { background: var(--hover); }
.layer.on { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.layer.clipped { padding-left: 18px; }
.layer.clipped .ltype::before { content: '↳'; position: absolute; margin-left: -13px; color: var(--accent); }
.layer.hidden .lname, .layer.hidden .ltype { opacity: .45; }
.eye { width: 26px; height: 26px; display: grid; place-items: center; color: var(--dim); border-radius: 4px; }
.eye:hover { color: var(--text); }
.ltype { color: var(--dim); display: grid; place-items: center; position: relative; }
.lname { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lmeta { color: var(--dim); font-size: 10px; white-space: nowrap; }
.lbadge { color: var(--dim); display: grid; place-items: center; }
.rename { flex: 1; background: var(--field); border: 1px solid var(--accent); border-radius: 3px; padding: 2px 4px; -webkit-user-select: text; user-select: text; }
.pnl-foot { display: flex; align-items: center; gap: 1px; padding: 3px 4px; border-top: 1px solid var(--line); background: var(--panel-2); flex: none; }
.ib.wide { padding: 0 6px; font-size: 11px; }

/* ---- pages ---- */
#pnl-pages { max-height: 30vh; }
#page-list { overflow-y: auto; padding: 8px; display: grid; gap: 8px; }
#page-list.spread { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
#page-list.spread.rtl { direction: rtl; }
#page-list.strip { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); }
.pg { display: flex; flex-direction: column; align-items: center; gap: 3px; direction: ltr; }
.pg canvas { width: 100%; height: auto; max-height: 120px; object-fit: contain; object-position: top; background: #fff; box-shadow: 0 0 0 1px var(--line); border-radius: 2px; }
.pg.on canvas { box-shadow: 0 0 0 2px var(--accent); }
.pg-n { font-size: 10px; color: var(--dim); }
.pg.on .pg-n { color: var(--accent); font-weight: 600; }

/* ---- modal ---- */
.modal-back { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.42); display: grid; place-items: center; padding: 16px; }
.modal { width: min(420px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); }
.modal.wide { width: min(760px, 100%); }
.modal > header { display: flex; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line-soft); }
.modal h3 { margin: 0; font-size: 13px; flex: 1; }
.modal .x { width: 26px; height: 26px; display: grid; place-items: center; border-radius: 4px; color: var(--dim); }
.modal .x:hover { background: var(--hover); color: var(--text); }
.modal-body { padding: 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 9px; }
.modal-body p { margin: 0; line-height: 1.5; }
.modal > footer { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line-soft); }
.np { display: flex; flex-direction: column; gap: 9px; }
.np .ctl { grid-template-columns: 110px 1fr; }
.np .ctl.stack { grid-template-columns: 1fr; }
.seg { display: flex; background: var(--field); border: 1px solid var(--line); border-radius: 5px; padding: 2px; gap: 2px; }
.seg.wrap { flex-wrap: wrap; }
.seg button { flex: 1; padding: 6px 8px; border-radius: 3px; white-space: nowrap; }
.seg button.on { background: var(--accent); color: #fff; }
.keys { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
.keys h4 { margin: 0 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--dim); }
.keys dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; }
.keys dt { font-family: ui-monospace, Menlo, monospace; font-size: 11px; background: var(--field); border: 1px solid var(--line); border-radius: 3px; padding: 1px 5px; justify-self: start; }
.keys dd { margin: 0; }

#toast { position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px); background: #1b1b1b; color: #eee; border: 1px solid #000; padding: 8px 14px; border-radius: 6px; opacity: 0; pointer-events: none; transition: .18s; z-index: 80; max-width: calc(100vw - 32px); box-shadow: var(--shadow); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.loading { position: fixed; inset: 0; z-index: 70; display: grid; place-items: center; background: rgba(0,0,0,.35); color: #fff; font-size: 14px; }

/* ---- home ---- */
#home { position: fixed; inset: 0; z-index: 40; background: var(--bg); overflow-y: auto; -webkit-user-select: none; user-select: none; }
.home-bar { display: flex; align-items: center; gap: 10px; padding: calc(10px + env(safe-area-inset-top)) 20px 10px; background: var(--bar); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; }
.logo.big { font-size: 18px; padding: 0; }
.logo.big:hover { background: none; }
.tag { color: var(--dim); }
.home-main { max-width: 1080px; margin: 0 auto; padding: 22px 20px 40px; }
.home-main h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); margin: 28px 0 12px; font-weight: 600; }
.start { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }
.start-card { display: flex; gap: 14px; align-items: center; text-align: left; padding: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
.start-card:hover { border-color: var(--accent); background: var(--panel-2); }
.start-card b { display: block; font-size: 14px; margin-bottom: 4px; }
.start-card span { color: var(--dim); line-height: 1.45; }
.art { flex: none; width: 62px; height: 84px; background: #fff; border-radius: 2px; box-shadow: 0 2px 6px rgba(0,0,0,.35); padding: 6px; display: grid; gap: 3px; }
.art i { border: 1.5px solid #111; }
.art.manga { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1.2fr 1fr; }
.art.manga i:first-child { grid-column: span 2; }
.art.manga i:nth-child(4) { grid-column: span 2; background-image: radial-gradient(#111 1px, transparent 1.2px); background-size: 4px 4px; }
.art.manga i:nth-child(5) { display: none; }
.art.manhwa { width: 40px; grid-template-rows: 1fr 1.4fr 1fr; gap: 9px; padding: 6px 4px; }
.art.manhwa i:nth-child(1) { background: #f2c9a9; }
.art.manhwa i:nth-child(2) { background: #8fb7e8; }
.art.manhwa i:nth-child(3) { background: #f5d547; }
.art.sample { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr 1.3fr; }
.art.sample i:first-child { grid-column: span 2; background: linear-gradient(#bbb, #fff); }
.art.sample i:last-child { grid-column: span 2; background: repeating-conic-gradient(#111 0 4deg, #fff 4deg 14deg); }
#proj-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
.empty { color: var(--dim); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; display: flex; flex-direction: column; }
.card:hover { border-color: var(--line-soft); }
.card-open { text-align: left; display: flex; flex-direction: column; gap: 3px; padding: 10px 10px 6px; }
.card-thumb { height: 180px; background: var(--canvas-bg); border-radius: 3px; display: grid; place-items: center; overflow: hidden; margin-bottom: 6px; }
.card-thumb img { max-width: 100%; max-height: 100%; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.card-thumb.tall img { width: 70%; max-height: none; align-self: start; }
.card-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-meta { color: var(--dim); font-size: 11px; }
.card-acts { display: flex; gap: 2px; padding: 0 6px 6px; }
.card-acts .ib { font-size: 11px; color: var(--dim); }
.card-acts .ib:hover { color: var(--text); }
.card-acts .ib.danger { margin-left: auto; }
.home-foot { margin-top: 40px; color: var(--dim); font-size: 11px; line-height: 1.6; }

/* ---- narrow screens (iPad portrait, phones): side panels become drawers ---- */
@media (max-width: 980px) {
  .narrow-only { display: inline-flex; }
  #main { grid-template-columns: var(--tb-w) 1fr; position: relative; }
  #left, #right { position: absolute; top: 0; bottom: 0; z-index: 30; width: min(280px, calc(100vw - var(--tb-w) - 16px)); box-shadow: var(--shadow); display: none; }
  #left { left: var(--tb-w); }
  #right { right: 0; }
  body.show-left #left, body.show-right #right { display: flex; }
  body.no-panels #main { grid-template-columns: var(--tb-w) 1fr; }
  #menus .menu-btn { padding: 5px 6px; }
  #doc-title, .tag { display: none; }
}
@media (max-width: 560px) {
  #btn-export, #save-state { display: none; }
  .logo span { display: none; }
}

/* right column scrolls as a whole so the layer list never gets squeezed */
#right { overflow-y: auto; }
#pnl-layers { flex: 1 0 auto; }
#layer-list { flex: 1 0 auto; min-height: 150px; max-height: 42vh; }
#pnl-pages { flex: none; max-height: 28vh; }
