:root {
  --bg: #f5efe4;
  --surface: #fffaf2;
  --surface-2: #efe6d6;
  --ink: #3a2e28;
  --muted: #7a6a5e;
  --line: #e0d4c2;
  --accent: #b85c38;
  --accent-ink: #ffffff;
  --accent-soft: #f3dccf;
  --ok: #4f7a4a;
  --ok-soft: #dfead6;
  --warn: #9a6a12;
  --warn-soft: #f4e6c4;
  --danger: #a33a2f;
  --shadow: 0 1px 2px rgba(58, 46, 40, .06), 0 6px 18px rgba(58, 46, 40, .07);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --radius: 14px;
  --tap: 44px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1916;
    --surface: #27211d;
    --surface-2: #322a25;
    --ink: #efe5d8;
    --muted: #b3a493;
    --line: #3e352f;
    --accent: #e08a63;
    --accent-ink: #1d1916;
    --accent-soft: #4a3026;
    --ok: #9cc58f;
    --ok-soft: #2d3a28;
    --warn: #e2bb6a;
    --warn-soft: #43361c;
    --danger: #ec8b7f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 var(--sans);
  padding: env(safe-area-inset-top, 0) 0 env(safe-area-inset-bottom, 0);
}
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.7rem; } h2 { font-size: 1.3rem; } h3 { font-size: 1.1rem; }
a { color: var(--accent); }
img { max-width: 100%; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 8px 16px; display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.brand { font-family: var(--serif); font-size: 1.25rem; font-weight: 600; color: var(--ink); text-decoration: none; display: flex; align-items: center; gap: 8px; }
.brand-mark { width: 20px; height: 28px; border-radius: 4px; background: var(--accent); box-shadow: 3px 3px 0 var(--accent-soft); }
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav a {
  min-height: var(--tap); display: inline-flex; align-items: center; padding: 0 14px; border-radius: 999px;
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.nav a.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
main { max-width: 1200px; margin: 0 auto; padding: 20px 16px 64px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; margin-right: auto; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- controls ---------- */
button, .btn {
  font: inherit; min-height: var(--tap); padding: 0 16px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; font-weight: 500;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
button:hover, .btn:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.primary:hover { filter: brightness(1.05); border-color: var(--accent); }
.danger { color: var(--danger); }
.ghost { background: transparent; border-color: transparent; }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  min-height: var(--tap); padding: 8px 12px; width: 100%;
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: block; }
label.field > span { display: flex; justify-content: space-between; font-size: .875rem; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
.counter.over { color: var(--danger); font-weight: 600; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 520px) { .grid-2 { grid-template-columns: 1fr; } }

/* ---------- cards page ---------- */
.stats { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px 14px; min-width: 96px; }
.stat b { display: block; font-family: var(--serif); font-size: 1.4rem; line-height: 1.1; }
.stat span { font-size: .8rem; color: var(--muted); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  min-height: 36px; padding: 0 14px; border-radius: 999px; font-size: .9rem;
  background: transparent; border: 1px solid var(--line); color: var(--muted);
}
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.filters { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)) minmax(0, 2fr); gap: 8px; margin: 12px 0 20px; }
@media (max-width: 760px) { .filters { grid-template-columns: 1fr 1fr; } .filters .search { grid-column: 1 / -1; } }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; }
@media (max-width: 420px) { .card-grid { grid-template-columns: 1fr 1fr; gap: 12px; } }
.tile { text-decoration: none; color: inherit; display: block; }
.tile-art {
  position: relative; aspect-ratio: 5 / 7; border-radius: 12px; overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.tile-art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile-art .empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: .85rem; text-align: center; padding: 12px; }
.tile:hover .tile-art { outline: 2px solid var(--accent); outline-offset: 2px; }
.tile-title { font-family: var(--serif); font-weight: 600; margin-top: 8px; line-height: 1.25; overflow-wrap: anywhere; }
.tile-meta { font-size: .8rem; color: var(--muted); }
.badge {
  display: inline-block; font-size: .72rem; font-weight: 600; letter-spacing: .02em; text-transform: uppercase;
  padding: 3px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted);
}
.badge.published { background: var(--ok-soft); color: var(--ok); }
.badge.draft { background: var(--accent-soft); color: var(--accent); }
.badge.soft { background: var(--warn-soft); color: var(--warn); }
.tile-art .badges { position: absolute; top: 8px; left: 8px; right: 8px; display: flex; gap: 4px; flex-wrap: wrap; }
.tile-art .badge { box-shadow: 0 1px 3px rgba(0,0,0,.15); background: var(--surface); }
.tile-art .badge.published { background: var(--ok-soft); }
.tile-art .badge.draft { background: var(--accent-soft); }
.tile-art .badge.soft { background: var(--warn-soft); }
.empty-state { text-align: center; padding: 48px 16px; color: var(--muted); }

/* ---------- editor ---------- */
.editor { display: grid; grid-template-columns: minmax(280px, 420px) minmax(0, 1fr); gap: 24px; align-items: start; }
@media (max-width: 820px) { .editor { grid-template-columns: 1fr; } }
.crop-col { position: sticky; top: 76px; }
@media (max-width: 820px) { .crop-col { position: static; } }
.frame {
  position: relative; aspect-ratio: 5 / 7; width: 100%; max-width: 420px; margin: 0 auto;
  border-radius: 14px; overflow: hidden; background: var(--surface-2); border: 1px solid var(--line);
  touch-action: none; user-select: none; -webkit-user-select: none; cursor: grab; box-shadow: var(--shadow);
}
.frame.dragging { cursor: grabbing; }
.frame img.src { position: absolute; left: 0; top: 0; max-width: none; transform-origin: 0 0; pointer-events: none; -webkit-user-drag: none; }
.frame .placeholder { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 24px; color: var(--muted); cursor: default; }
.frame .guides { position: absolute; inset: 0; pointer-events: none; box-shadow: inset 0 0 0 1px rgba(255,255,255,.35); }
.frame .loading { position: absolute; inset: auto 8px 8px auto; }
.crop-tools { max-width: 420px; margin: 12px auto 0; }
.zoom { display: flex; align-items: center; gap: 10px; }
.zoom input[type=range] { padding: 0; border: 0; background: transparent; accent-color: var(--accent); min-height: var(--tap); }
.res { font-size: .875rem; }
.res .badge { margin-right: 6px; }
.preview {
  background: var(--surface-2); border-radius: 14px; padding: 20px; border: 1px dashed var(--line); text-align: center;
}
.preview .p-title { font-family: var(--serif); font-size: 1.35rem; font-weight: 600; margin-bottom: 6px; overflow-wrap: anywhere; }
.preview .p-task { font-family: var(--serif); font-size: 1.05rem; font-style: italic; color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; }
.preview .p-meta { margin-top: 10px; font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.status-line { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions .spacer { flex: 1; }
.missing { font-size: .875rem; color: var(--warn); }

/* ---------- packs + settings ---------- */
.pack { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 12px; }
@media (max-width: 640px) { .pack { grid-template-columns: 1fr; } }
.thumbs { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.thumbs img, .thumbs .ph { width: 46px; aspect-ratio: 5 / 7; border-radius: 6px; object-fit: cover; border: 1px solid var(--line); background: var(--surface-2); }
.pack-actions { display: flex; gap: 8px; flex-wrap: wrap; align-content: start; justify-content: flex-end; }
.list { list-style: none; padding: 0; margin: 0; }
.list li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.list li:last-child { border-bottom: 0; }
.list .name { flex: 1; min-width: 120px; font-weight: 500; }
.spec { margin: 0; padding-left: 20px; }
.spec li { margin: 2px 0; }
code { font-size: .9em; background: var(--surface-2); padding: 1px 6px; border-radius: 6px; overflow-wrap: anywhere; }

/* ---------- toasts ---------- */
.toasts { position: fixed; z-index: 50; left: 50%; transform: translateX(-50%); bottom: calc(16px + env(safe-area-inset-bottom, 0px)); display: flex; flex-direction: column; gap: 8px; width: min(92vw, 440px); }
.toast {
  background: var(--ink); color: var(--bg); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow);
  font-size: .95rem; animation: rise .2s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login { width: min(100%, 380px); text-align: center; }
.login .brand-mark { width: 40px; height: 56px; margin: 0 auto 16px; border-radius: 8px; box-shadow: 6px 6px 0 var(--accent-soft); }
.login form { text-align: left; margin-top: 20px; }
.login .error { color: var(--danger); min-height: 1.5em; font-size: .9rem; margin: 8px 0; }
.login button { width: 100%; }
