*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, sans-serif;
  background: #f5f0ea;
  color: #1c1b1f;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ── toolbar ── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: #d4c5b2;
  flex-shrink: 0;
  flex-wrap: wrap;
}
#toolbar h1 { font-size: 18px; font-weight: 700; margin-right: 8px; }

#server-url {
  padding: 4px 8px;
  border: 1px solid #9e8e7a;
  border-radius: 4px;
  font-size: 13px;
  width: 220px;
  background: #fff;
}

.btn {
  padding: 6px 14px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}
.btn-primary { background: #8b7d6b; color: #fff; }
.btn-primary:hover { background: #7a6c5c; }
.btn-primary:disabled { background: #c4b9ad; cursor: default; }
.btn-sm {
  padding: 2px 8px;
  font-size: 12px;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  font-weight: 600;
  background: #c4b9ad;
  color: #1c1b1f;
}
.btn-sm:hover { background: #b0a496; }
.btn-sm:disabled { opacity: .45; cursor: default; }
#lang-select {
  padding: 2px 6px;
  font-size: 12px;
  border: 1px solid #9e8e7a;
  border-radius: 3px;
  cursor: pointer;
  font-weight: 600;
  background: #c4b9ad;
  color: #1c1b1f;
  height: 22px;
}

#hint {
  font-size: 12px;
  color: #5a4e42;
  margin-left: auto;
}

/* ── shared view wrapper ── */
.view {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}
.view.hidden { display: none; }

/* ── home view ── */
#home-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
#home-header h2 {
  font-size: 16px;
  font-weight: 700;
  color: #5a4e42;
  text-transform: uppercase;
  letter-spacing: .06em;
}
#home-empty {
  text-align: center;
  color: #9e8e7a;
  margin-top: 60px;
  font-size: 15px;
  line-height: 1.7;
}
#home-empty strong { color: #5a4e42; }

#home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
}

.page-card {
  background: #fff;
  border-radius: 8px;
  border: 2px solid transparent;
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
}
.page-card:hover {
  border-color: #9e8e7a;
  box-shadow: 0 3px 10px rgba(0,0,0,.10);
}

.card-thumb {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: cover;
  background: #d4c5b2;
  display: block;
}

.card-body {
  padding: 8px 8px 4px;
}
.card-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-meta {
  font-size: 11px;
  color: #8b7d6b;
  margin-top: 3px;
  line-height: 1.4;
}
.card-actions {
  display: flex;
  gap: 6px;
  padding: 6px 8px 8px;
}

/* ── doc-picker view ── */
#doc-picker-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
#doc-picker-title {
  font-size: 16px;
  font-weight: 700;
  color: #5a4e42;
}
#doc-picker-status {
  font-size: 12px;
  color: #5a4e42;
  margin-right: auto;
}
#doc-picker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 12px;
}
.doc-page-card {
  background: #fff;
  border-radius: 8px;
  border: 2px solid transparent;
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
  user-select: none;
}
.doc-page-card:hover { border-color: #9e8e7a; box-shadow: 0 3px 10px rgba(0,0,0,.10); }
.doc-page-card.selected { border-color: #1e64ff; box-shadow: 0 0 0 2px rgba(30,100,255,.2); }
.doc-page-thumb {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: contain;
  background: #d4c5b2;
  display: block;
}
.doc-page-num {
  padding: 4px 8px 6px;
  font-size: 11px;
  font-weight: 600;
  color: #5a4e42;
  text-align: center;
}

/* ── versions view ── */
#versions-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
  padding-bottom: 14px;
  border-bottom: 2px solid #1c1b1f;
}
#versions-title {
  font-size: 20px;
  font-weight: 700;
  color: #1c1b1f;
}
#versions-status {
  font-size: 12px;
  color: #5a4e42;
  margin-right: auto;
}
#versions-empty {
  text-align: center;
  color: #9e8e7a;
  margin-top: 60px;
  font-size: 15px;
  line-height: 1.7;
}
#versions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.ver-card {
  background: #fff;
  border-radius: 8px;
  border: 2px solid transparent;
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
}
.ver-card:hover { border-color: #9e8e7a; box-shadow: 0 3px 10px rgba(0,0,0,.10); }

.ver-canvas {
  width: 100%;
  display: block;
  background: #d4c5b2;
}

.ver-info {
  padding: 8px 10px 4px;
}
.ver-num {
  font-size: 13px;
  font-weight: 700;
  color: #1c1b1f;
}
.ver-time {
  font-size: 11px;
  color: #8b7d6b;
  margin-top: 1px;
}
.ver-commit {
  font-size: 11px;
  color: #8b7d6b;
  font-family: monospace;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ver-actions {
  display: flex;
  gap: 6px;
  padding: 6px 8px 8px;
}

/* ── main area (reader view) ── */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
}
#main.hidden { display: none; }

/* ── zoom controls ── */
#zoom-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}
#zoom-level-label {
  font-size: 12px;
  min-width: 3.5em;
  text-align: center;
  color: #1c1b1f;
}

/* ── canvas panel ── */
#canvas-panel {
  flex: 1 1 60%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  overflow: auto;
  padding: 16px;
  position: relative;
}
#main.reflow-mode #canvas-panel { flex: 1 1 50%; }
#main.reflow-mode #text-panel   { flex: 1 1 50%; }

#drop-zone {
  flex: 1;
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed #9e8e7a;
  border-radius: 8px;
  color: #9e8e7a;
  font-size: 15px;
  text-align: center;
  line-height: 1.6;
  cursor: pointer;
}
#drop-zone.hidden { display: none; }

#canvas-wrap {
  position: relative;
  display: none;
  margin: 0 auto;
}
#canvas-wrap.visible { display: inline-block; }

#page-canvas {
  display: block;
  border: 1px solid #c4b9ad;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
}

/* legend */
#legend {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  font-size: 12px;
  flex-wrap: wrap;
}
.legend-item { display: flex; align-items: center; gap: 4px; }
.legend-swatch {
  width: 14px; height: 14px;
  border-radius: 2px;
  border: 2px solid;
}

/* ── text panel ── */
#text-panel {
  flex: 0 0 640px;
  border-left: 1px solid #c4b9ad;
  display: flex;
  flex-direction: column;
  background: #fff;
  overflow: hidden;
}

#text-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: #ece7e0;
  border-bottom: 1px solid #c4b9ad;
  flex-shrink: 0;
  gap: 8px;
  flex-wrap: wrap;
}
#text-panel-header h2 {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

/* panel tabs */
#panel-tabs {
  display: flex;
  gap: 2px;
  margin-left: 4px;
}
.panel-tab {
  padding: 2px 10px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid #c4b9ad;
  border-radius: 3px 3px 0 0;
  cursor: pointer;
  background: #d4c5b2;
  color: #5a4e42;
}
.panel-tab.active {
  background: #fff;
  border-bottom-color: #fff;
  color: #1c1b1f;
}

#version-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}
#version-label {
  font-size: 11px;
  color: #5a4e42;
  white-space: nowrap;
}
#version-bar.hidden { display: none; }

#text-output {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
#json-output {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  font-size: 11px;
  font-family: monospace;
  line-height: 1.5;
  white-space: pre;
  word-break: break-all;
  background: #faf9f7;
  display: none;
}
#reflow-output {
  flex: 1;
  overflow-y: auto;
  display: none;
}
#reflow-canvas { display: block; }
#reflow-controls {
  display: none;
  padding: 6px 14px;
  border-bottom: 1px solid #c4b9ad;
  background: #ece7e0;
  flex-shrink: 0;
}
.rc-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: #5a4e42;
  margin-bottom: 3px;
}
.rc-row:last-child { margin-bottom: 0; }
.rc-label { width: 46px; flex-shrink: 0; }
.rc-row input[type=range] { flex: 1; }
.rc-val { min-width: 2.6em; text-align: right; flex-shrink: 0; }
.align-btns { display: flex; gap: 3px; }
.align-btn {
  padding: 1px 7px;
  font-size: 12px;
  border: 1px solid #c4b9ad;
  border-radius: 3px;
  cursor: pointer;
  background: #d4c5b2;
  color: #5a4e42;
  line-height: 1.6;
}
.align-btn.active { background: #8b7d6b; color: #fff; border-color: #8b7d6b; }

.text-block {
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0ece6;
}
.text-block:last-child { border-bottom: none; }

.block-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  margin-bottom: 4px;
}

#status {
  font-size: 12px;
  padding: 6px 14px;
  background: #f5f0ea;
  border-top: 1px solid #c4b9ad;
  flex-shrink: 0;
  min-height: 28px;
}

/* spinner */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  display: inline-block;
  width: 12px; height: 12px;
  border: 2px solid #8b7d6b;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: middle;
  margin-right: 6px;
}
