:root {
  --paper: #fbfbf8;
  --paper-2: #f0f0ec;
  --ink: #000;
  --ink-soft: #4a4a4a;
  --tone: #c4c4c4;
  --blue: #8fd0ee;          /* non-photo blue: marks what the translation did */
  --blue-ink: #13628f;      /* readable blue for text on paper */
  --red: #c8262b;           /* editor's red pen: errors only */
  --gutter: 3px;
  --font-display: "Dela Gothic One", "Arial Black", sans-serif;
  --font-body: "Onest", system-ui, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #141414;
    --paper-2: #1f1f1f;
    --ink: #f4f4f0;
    --ink-soft: #b0b0aa;
    --tone: #4a4a4a;
    --blue: #3d8fb8;
    --blue-ink: #8fd0ee;
    --red: #ff6b6f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

/* component display rules (flex/grid) must not defeat the hidden attribute */
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font: 400 1rem/1.55 var(--font-body);
  min-height: 100vh;
}

:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 20px clamp(16px, 4vw, 48px);
  border-bottom: var(--gutter) solid var(--ink);
}

.logo {
  font: 400 clamp(1.75rem, 4vw, 2.25rem)/1 var(--font-display);
  color: var(--ink);
  text-decoration: none;
  letter-spacing: -0.01em;
}

.top-controls { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }

.lang { display: flex; align-items: center; gap: 8px; font-size: 0.9375rem; color: var(--ink-soft); }

select {
  font: 500 1rem var(--font-body);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 0;
  padding: 6px 10px;
}

.server { font-size: 0.875rem; color: var(--ink-soft); display: inline-flex; align-items: center; gap: 8px; }
.server::before {
  content: "";
  width: 10px; height: 10px; border-radius: 50%;
  border: 2px solid var(--ink);
  background: var(--paper);
}
.server[data-state="online"]::before { background: var(--blue); }
.server[data-state="offline"] { color: var(--red); }
.server[data-state="offline"]::before { background: var(--red); border-color: var(--red); }

/* ---------- main ---------- */
main { padding: clamp(16px, 4vw, 48px); max-width: 1280px; margin: 0 auto; }

/* drop zone: a manga panel with a screentone corner */
.drop {
  position: relative;
  border: var(--gutter) solid var(--ink);
  padding: clamp(32px, 7vw, 88px) clamp(20px, 5vw, 64px);
  background: var(--paper);
  overflow: hidden;
  transition: background-color 0.15s;
}
.drop::after {
  content: "";
  position: absolute;
  right: 0; bottom: 0;
  width: min(48%, 420px);
  height: 75%;
  background-image: radial-gradient(var(--tone) 28%, transparent 30%);
  background-size: 9px 9px;
  -webkit-mask-image: linear-gradient(135deg, transparent 30%, #000 75%);
          mask-image: linear-gradient(135deg, transparent 30%, #000 75%);
  pointer-events: none;
}
.drop.is-over { background: color-mix(in srgb, var(--blue) 22%, var(--paper)); }
.drop.is-over::after { background-image: radial-gradient(var(--blue) 30%, transparent 32%); }
.drop.is-compact { padding: 20px clamp(20px, 4vw, 32px); }
.drop.is-compact h1 { font-size: 1.375rem; }
.drop.is-compact p { display: none; }

.drop h1 {
  position: relative;
  z-index: 1;
  font: 400 clamp(1.875rem, 5.5vw, 3.5rem)/1.08 var(--font-display);
  margin: 0 0 16px;
  max-width: 14ch;
}
.drop p { position: relative; z-index: 1; margin: 0 0 28px; max-width: 46ch; color: var(--ink-soft); font-size: 1.0625rem; }
.drop .btn { position: relative; z-index: 1; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: 500 1rem/1 var(--font-body);
  padding: 12px 18px;
  border: 2px solid var(--ink);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
  min-height: 44px;
}
.btn:hover { background: var(--paper-2); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-ink { background: var(--ink); color: var(--paper); }
.btn-ink:hover { background: var(--ink-soft); }
.btn-quiet { border-color: transparent; text-decoration: underline; text-underline-offset: 3px; }
.btn-quiet:hover { background: transparent; color: var(--ink-soft); }

/* ---------- work area ---------- */
.work { margin-top: 32px; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.summary { margin: 0; font-weight: 500; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.view-switch { display: flex; gap: 24px; margin: 24px 0 16px; border-bottom: 2px solid var(--ink); }
.view-switch button {
  font: 400 1.125rem var(--font-display);
  background: none; border: 0; color: var(--ink-soft);
  padding: 8px 0; margin-bottom: -2px; cursor: pointer;
  border-bottom: 4px solid transparent;
}
.view-switch button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--ink); }

/* grid of pages, separated by panel gutters */
.grid {
  list-style: none; margin: 0; padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
}

.tile { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.tile-open {
  position: relative;
  display: block;
  padding: 0;
  border: var(--gutter) solid var(--ink);
  background: var(--paper-2);
  aspect-ratio: 5 / 7;
  overflow: hidden;
  cursor: pointer;
}
.tile-img { width: 100%; height: 100%; object-fit: cover; display: block; }

.tile-state {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 6px 8px;
  font-size: 0.8125rem; font-weight: 500; text-align: left;
  background: var(--paper); color: var(--ink);
  border-top: 2px solid var(--ink);
}
.tile[data-state="ready"] .tile-state { color: var(--ink-soft); }
.tile[data-state="work"] .tile-open { border-color: var(--blue-ink); }
.tile[data-state="work"] .tile-state { background: var(--blue); color: #000; }
.tile[data-state="work"] .tile-img { filter: grayscale(1) opacity(0.6); }
.tile[data-state="done"] .tile-state { display: none; }
.tile[data-state="done"] .tile-open::after {
  /* small blue-pencil tick in the corner: this page is translated */
  content: ""; position: absolute; top: 0; right: 0;
  border-style: solid; border-width: 0 28px 28px 0;
  border-color: transparent var(--blue) transparent transparent;
}
.tile[data-state="error"] .tile-open { border-color: var(--red); }
.tile[data-state="error"] .tile-state { color: var(--red); white-space: normal; }

.tile-foot { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.tile-name { font-size: 0.8125rem; color: var(--ink-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-retry { font: 500 0.8125rem var(--font-body); color: var(--red); background: none; border: 0; padding: 0; text-decoration: underline; cursor: pointer; }

@media (prefers-reduced-motion: no-preference) {
  .tile[data-state="work"] .tile-state {
    background-image: radial-gradient(rgb(0 0 0 / 0.18) 30%, transparent 32%);
    background-size: 7px 7px;
    animation: tone 1.2s linear infinite;
  }
  @keyframes tone { to { background-position: 14px 0; } }
}

/* reader: pages stacked like a webtoon */
.reader { max-width: 900px; margin: 0 auto; display: flex; flex-direction: column; gap: 0; }
.reader figure { margin: 0; position: relative; border-bottom: var(--gutter) solid var(--ink); }
.reader img { display: block; width: 100%; height: auto; }
.reader .missing { padding: 48px 16px; text-align: center; color: var(--ink-soft); background: var(--paper-2); }
.reader-empty { color: var(--ink-soft); padding: 32px 0; }

/* ---------- viewer with before/after shutter ---------- */
.viewer {
  width: min(100vw - 32px, 960px);
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 0;
  border: var(--gutter) solid var(--ink);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
}
.viewer::backdrop { background: rgb(0 0 0 / 0.7); }
.viewer-bar {
  position: sticky; top: 0; z-index: 3;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 12px; background: var(--paper); border-bottom: 2px solid var(--ink);
}
#viewer-title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-hint { margin: 0; padding: 10px 12px; font-size: 0.875rem; color: var(--ink-soft); }

.compare { position: relative; --pos: 50%; user-select: none; touch-action: pan-y; }
.compare img { display: block; width: 100%; height: auto; }
.cmp-before-wrap { position: absolute; inset: 0; clip-path: inset(0 calc(100% - var(--pos)) 0 0); }
.cmp-before-wrap img { width: 100%; height: 100%; object-fit: contain; }
.cmp-handle {
  position: absolute; top: 0; bottom: 0; left: var(--pos);
  width: 4px; margin-left: -2px; background: var(--blue);
  box-shadow: 0 0 0 1px #000;
  pointer-events: none;
}
.cmp-handle::after {
  content: ""; position: absolute; top: 50%; left: 50%;
  width: 28px; height: 28px; margin: -14px 0 0 -14px;
  border-radius: 50%; background: var(--blue); border: 2px solid #000;
}
.cmp-range {
  position: absolute; inset: 0; width: 100%; height: 100%;
  margin: 0; opacity: 0; cursor: ew-resize;
}
.compare.no-before .cmp-before-wrap,
.compare.no-before .cmp-handle,
.compare.no-before .cmp-range { display: none; }

@media (max-width: 560px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .toolbar .actions { width: 100%; }
  .toolbar .actions .btn { flex: 1 1 auto; }
  .lang span { display: none; }
}
