:root {
  --paper: #f3f4f7;
  --sheet: #ffffff;
  --ink: #1d2433;
  --muted: #5b6475;
  --line: #d6dbe3;
  --seal: #b3261e;
  --seal-ink: #ffffff;
  --seal-soft: #fbe9e7;
  --warn-bg: #fff4d6;
  --warn-line: #e7c35a;
  --err-bg: #fde8e8;
  --shadow: 0 1px 2px rgba(29, 36, 51, .08), 0 4px 14px rgba(29, 36, 51, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #14171d;
    --sheet: #1c2028;
    --ink: #e8eaef;
    --muted: #a0a8b8;
    --line: #2f3542;
    --seal: #e5534b;
    --seal-ink: #1a0b0a;
    --seal-soft: #3a1d1b;
    --warn-bg: #3a3118;
    --warn-line: #8a7224;
    --err-bg: #3d1e1e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
h1, h2 { margin: 0; line-height: 1.2; }

/* Thanh trên */
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 24px; background: var(--sheet); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 18px; font-weight: 650; letter-spacing: -.01em; }
.promise { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.seal { width: 34px; height: 34px; flex: none; fill: none; stroke: var(--seal); stroke-width: 3.5; stroke-linecap: round; stroke-linejoin: round; }
.bar-actions { display: flex; gap: 8px; flex: none; }

/* Nút */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: 14px;
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  background: var(--sheet); color: var(--ink); border: 1px solid var(--line);
  white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--seal); color: var(--seal-ink); border-color: var(--seal); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--ink); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:focus-visible, .page:focus-visible, .icon:focus-visible, input:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--seal) 55%, transparent); outline-offset: 2px;
}

main { flex: 1; width: 100%; max-width: 1180px; margin: 0 auto; padding: 24px 24px 120px; }

/* Thông báo */
#alerts { display: grid; gap: 8px; margin-bottom: 16px; }
.alert { display: flex; gap: 12px; align-items: flex-start; padding: 10px 14px; border-radius: 8px; border: 1px solid var(--warn-line); background: var(--warn-bg); font-size: 14px; }
.alert.error { border-color: var(--seal); background: var(--err-bg); }
.alert p { margin: 0; flex: 1; }
.alert button { background: none; border: 0; color: inherit; cursor: pointer; font-size: 18px; line-height: 1; padding: 0 2px; }

/* Màn trống */
.empty { display: grid; place-items: center; min-height: 60vh; }
.drop-target {
  text-align: center; max-width: 520px; padding: 48px 32px;
  border: 2px dashed var(--line); border-radius: 16px; background: var(--sheet);
}
.drop-target svg { width: 64px; height: 64px; fill: none; stroke: var(--muted); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.drop-target h2 { font-size: 22px; margin: 14px 0 8px; font-weight: 650; }
.drop-target p { color: var(--muted); margin: 0 0 20px; }

/* Khu làm việc */
.ws-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 16px; align-items: baseline; margin-bottom: 14px; }
#summary { margin: 0; font-weight: 600; }
.hint { margin: 0; color: var(--muted); font-size: 13px; }

.grid {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 18px;
}
.page {
  position: relative; background: var(--sheet); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 10px 8px; box-shadow: var(--shadow); cursor: grab; user-select: none;
  display: flex; flex-direction: column; gap: 8px;
}
.page.dragging { opacity: .4; }
.page.drop-before::before, .page.drop-after::after {
  content: ""; position: absolute; top: 6px; bottom: 6px; width: 4px; border-radius: 2px; background: var(--seal);
}
.page.drop-before::before { left: -12px; }
.page.drop-after::after { right: -12px; }
.page.selected { border-color: var(--seal); box-shadow: 0 0 0 2px var(--seal); }

.thumb {
  aspect-ratio: 210 / 297; display: grid; place-items: center; overflow: hidden;
  background: var(--paper); border-radius: 4px;
}
.thumb canvas, .thumb img { max-width: 100%; max-height: 100%; display: block; background: #fff; box-shadow: 0 0 0 1px var(--line); transition: transform .2s ease; }
.thumb .loading { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--muted); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.pos {
  position: absolute; top: -9px; left: -9px; min-width: 26px; height: 26px; padding: 0 7px;
  border-radius: 13px; background: var(--ink); color: var(--paper);
  font-size: 12px; font-weight: 700; display: grid; place-items: center;
}
.page.selected .pos { background: var(--seal); color: var(--seal-ink); }

.meta { font-size: 12px; color: var(--muted); line-height: 1.3; min-height: 2.6em; word-break: break-word; }
.meta strong { color: var(--ink); font-weight: 600; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 0 6px; border-radius: 4px; background: var(--seal-soft); color: var(--seal); margin-left: 4px; }

.tools { display: flex; justify-content: space-between; gap: 2px; }
.icon {
  flex: 1; height: 30px; border-radius: 6px; border: 1px solid transparent; background: transparent;
  color: var(--muted); cursor: pointer; display: grid; place-items: center;
}
.icon:hover { background: var(--paper); color: var(--ink); }
.icon.danger:hover { color: var(--seal); }
.icon svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon:disabled { opacity: .3; cursor: default; background: transparent; }

/* Thanh dưới */
.dock {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 24px; background: var(--sheet); border-top: 1px solid var(--line);
}
.name-field { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); flex: 1 1 280px; max-width: 460px; }
.name-field input {
  flex: 1; min-width: 0; font: inherit; color: var(--ink); background: var(--paper);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
.name-field .ext { color: var(--muted); }
.dock-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.drop-overlay {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center;
  background: color-mix(in srgb, var(--paper) 80%, transparent); border: 4px dashed var(--seal);
}
.drop-overlay p { font-size: 22px; font-weight: 650; }

[hidden] { display: none !important; }

@media (max-width: 640px) {
  .bar { padding: 12px 16px; flex-wrap: wrap; }
  .promise { display: none; }
  main { padding: 16px 16px 160px; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 14px; }
  .dock { padding: 12px 16px; }
  .dock-actions { width: 100%; }
  .dock-actions .btn { flex: 1; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
