:root {
  --navy: #142638;
  --navy-2: #1d354c;
  --steel: #526f88;
  --silver: #b9c4ce;
  --paper: #f5f7f9;
  --white: #ffffff;
  --muted: #627383;
  --line: #dbe3ea;
  --accent: #2b6cb0;
  --accent-soft: #e8f0fa;
  --danger: #b42318;
  --shadow: 0 10px 30px rgba(20, 38, 56, .08);
  --radius: 14px;
  --font: "Segoe UI", Roboto, Arial, system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--navy);
  background: var(--paper);
  display: flex;
  flex-direction: column;
  min-height: 100svh;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px clamp(16px, 3vw, 34px);
  background: var(--white);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: inline-flex; align-items: center; line-height: 0; }
.brand img { width: clamp(126px, 15vw, 154px); height: auto; }
.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.local-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  font-weight: 600;
  color: var(--steel);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 11px;
  white-space: nowrap;
}
.local-pill:hover { border-color: var(--silver); background: var(--white); }
.sync-pill { appearance: none; cursor: pointer; }
.sync-pill[data-state="ready"] .dot { background: #2f9e6e; box-shadow: 0 0 0 3px #dff3e8; }
.sync-pill[data-state="working"] .dot { background: #b26a00; box-shadow: 0 0 0 3px #fbedd8; }
.sync-pill[data-state="error"] .dot { background: var(--danger); box-shadow: 0 0 0 3px #fde8e7; }
.sync-pill[data-state="off"] .dot { background: var(--silver); box-shadow: 0 0 0 3px #eef2f5; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border-radius: 10px;
  border: 1px solid transparent;
  padding: 10px 16px;
  font-weight: 650;
  font-size: 14px;
  text-decoration: none;
  transition: background .15s ease, border-color .15s ease, transform .06s ease;
  user-select: none;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--navy); color: var(--white); }
.btn.primary:hover { background: var(--navy-2); }
.btn.ghost { background: transparent; color: var(--navy); border-color: var(--line); }
.btn.ghost:hover { background: var(--white); border-color: var(--silver); }
.btn.small { padding: 7px 12px; font-size: 12.5px; border-radius: 9px; }
.btn[disabled] { opacity: .45; cursor: default; }

main { flex: 1; min-height: 0; display: flex; }
.view { flex: 1; min-width: 0; min-height: 0; }

/* Library */
.library-view {
  width: 100%;
  padding: clamp(22px, 4vw, 48px);
  overflow: auto;
}
.hero {
  max-width: 900px;
  padding-bottom: clamp(22px, 4vw, 42px);
}
.eyebrow {
  text-transform: uppercase;
  letter-spacing: .13em;
  font-size: 11px;
  font-weight: 800;
  color: var(--steel);
  margin: 0 0 10px;
}
.hero h1 {
  font-size: clamp(34px, 6vw, 64px);
  line-height: .98;
  letter-spacing: -.035em;
  margin: 0 0 18px;
  color: var(--navy);
}
.hero .lead {
  font-size: clamp(15px, 1.6vw, 18px);
  line-height: 1.55;
  color: #33495d;
  max-width: 640px;
  margin: 0 0 22px;
}
.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.micro { font-size: 12px; color: var(--muted); margin: 16px 0 0; }

.library-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 22px;
  margin-bottom: 16px;
}
.library-head h2 { font-size: 19px; margin: 0; letter-spacing: -.01em; }
.library-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.muted { color: var(--muted); font-size: 13px; }

.doc-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 14px;
  padding-bottom: 40px;
}
.doc-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 168px;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 1px 0 rgba(20, 38, 56, .02);
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease, border-color .12s ease;
}
.doc-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); border-color: #c9d6e1; }
.doc-thumb {
  height: 72px;
  border-radius: 9px;
  background: var(--paper);
  border: 1px solid #edf1f5;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.doc-thumb img { width: 100%; height: 100%; object-fit: cover; }
.doc-type {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .12em;
  color: var(--steel);
  text-transform: uppercase;
}
.doc-name {
  font-weight: 650;
  font-size: 14.5px;
  line-height: 1.3;
  word-break: break-word;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.doc-meta { margin-top: auto; display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 11.5px; color: var(--muted); }
.card-actions { position: absolute; top: 9px; right: 9px; display: flex; gap: 3px; opacity: 0; transition: opacity .12s; }
.doc-card:hover .card-actions, .doc-card:focus-within .card-actions { opacity: 1; }
.chip-btn {
  border: 1px solid var(--line);
  background: var(--white);
  color: var(--steel);
  border-radius: 7px;
  width: 26px; height: 26px;
  display: grid; place-items: center;
  font-size: 13px;
  padding: 0;
}
.chip-btn:hover { color: var(--navy); border-color: var(--silver); }
.thumb-icon { font-size: 30px; color: var(--silver); line-height: 1; }

.empty-state { text-align: center; padding: 48px 20px 70px; color: var(--muted); }
.empty-state[hidden], .doc-grid[hidden] { display: none !important; }
.empty-icon { font-size: 44px; color: var(--silver); margin-bottom: 8px; }
.empty-state h3 { color: var(--navy); margin: 0 0 6px; font-size: 18px; }
.empty-state p { margin: 0 auto; max-width: 420px; font-size: 14px; line-height: 1.5; }

/* Viewer shell */
.document-view { display: flex; flex-direction: column; min-height: 0; background: #e8edf2; }
.document-view[hidden] { display: none !important; }
.viewer-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px clamp(10px, 2vw, 20px);
  background: var(--white);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
  z-index: 5;
}
.doc-title {
  font-weight: 700;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1 1 180px;
  min-width: 0;
}
.viewer-tools { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.divider { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.icon-btn {
  border: 1px solid transparent;
  background: transparent;
  color: var(--navy);
  width: 34px; height: 34px;
  border-radius: 9px;
  font-size: 20px;
  line-height: 1;
  display: grid; place-items: center;
  padding: 0;
}
.icon-btn:hover { background: var(--paper); border-color: var(--line); }
.icon-btn[disabled] { opacity: .35; cursor: default; }
.page-pill {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--paper); border: 1px solid var(--line);
  border-radius: 9px; padding: 3px 9px;
  font-size: 12.5px; font-weight: 650; color: var(--steel);
}
.page-pill input {
  width: 42px; border: 0; background: transparent; text-align: right;
  color: var(--navy); font-weight: 700; font-size: 12.5px; padding: 2px;
}
.page-pill input::-webkit-outer-spin-button,
.page-pill input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.zoom-label { min-width: 42px; text-align: center; font-size: 12px; font-weight: 700; color: var(--steel); }

.stage {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 22px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  position: relative;
}
#pdfCanvas {
  display: block;
  background: var(--white);
  box-shadow: 0 6px 28px rgba(20, 38, 56, .16);
  border-radius: 2px;
  max-width: 100%;
  height: auto;
}
.viewer-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--steel);
  font-size: 14px;
  background: rgba(232, 237, 242, .8);
  z-index: 2;
}
.viewer-loading[hidden] { display: none !important; }

.image-stage { align-items: center; }
#imageCanvas {
  max-width: min(100%, 1100px);
  max-height: 100%;
  object-fit: contain;
  box-shadow: 0 6px 28px rgba(20, 38, 56, .16);
  background: var(--white);
}

.text-stage { flex: 1; min-height: 0; padding: clamp(14px, 3vw, 32px); overflow: auto; background: var(--paper); }
#textEditor {
  display: block;
  width: min(880px, 100%);
  min-height: calc(100svh - 220px);
  margin: 0 auto;
  padding: clamp(22px, 4vw, 54px);
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--white);
  color: #1e293b;
  font-size: 15px;
  line-height: 1.7;
  resize: vertical;
  box-shadow: var(--shadow);
  outline: none;
}
#textEditor:focus { border-color: #b9cce0; }
.save-state { font-size: 11.5px; color: #2f7a58; font-weight: 650; }
.save-state.dirty { color: #b26a00; }

.app-footer {
  display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 9px clamp(16px, 3vw, 34px);
  border-top: 1px solid var(--line);
  background: var(--white);
  color: var(--muted);
  font-size: 11px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--navy);
  color: var(--white);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
  z-index: 100;
  max-width: min(90vw, 520px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.drop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 38, 56, .55);
  color: var(--white);
  display: grid;
  place-items: center;
  z-index: 90;
  font-size: 18px;
  border: 3px dashed rgba(255,255,255,.6);
  margin: 12px;
  border-radius: 18px;
}
.drop-overlay[hidden] { display: none !important; }

/* Sync modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 120;
  background: rgba(20, 38, 56, .48);
  display: grid;
  place-items: center;
  padding: 18px;
}
.modal[hidden] { display: none !important; }
.modal-card {
  width: min(620px, 100%);
  max-height: min(88vh, 820px);
  overflow: auto;
  background: var(--white);
  border-radius: 16px;
  box-shadow: 0 24px 70px rgba(20, 38, 56, .3);
  padding: clamp(18px, 3vw, 28px);
}
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.modal-head h2 { margin: 0; font-size: 24px; letter-spacing: -.02em; }
.modal-head .eyebrow { margin-bottom: 4px; }
.modal-lead { color: #33495d; font-size: 14px; line-height: 1.55; margin: 0 0 18px; }
.segmented {
  display: flex;
  gap: 4px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 18px;
}
.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  padding: 9px 10px;
  border-radius: 7px;
  font-weight: 650;
  color: var(--steel);
  font-size: 13px;
}
.segmented button.active { background: var(--white); color: var(--navy); box-shadow: 0 1px 4px rgba(20,38,56,.1); }
.sync-form { display: flex; flex-direction: column; gap: 13px; }
.sync-form[hidden] { display: none !important; }
.sync-form label { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; font-weight: 700; color: #33495d; }
.sync-form input[type="url"],
.sync-form input[type="password"],
.sync-form input[type="text"] {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 14px;
  color: var(--navy);
  background: var(--white);
  outline: none;
}
.sync-form input:focus { border-color: #9fbbd6; box-shadow: 0 0 0 3px #eaf2fa; }
.form-hint { font-size: 12px; line-height: 1.5; color: var(--muted); margin: 0; }
.form-row { display: flex; gap: 9px; flex-wrap: wrap; margin-top: 6px; }
.sync-summary { display: flex; gap: 12px; align-items: flex-start; padding: 14px; background: var(--paper); border: 1px solid var(--line); border-radius: 11px; margin-bottom: 16px; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--silver); margin-top: 5px; flex: none; }
.status-dot.ready { background: #2f9e6e; box-shadow: 0 0 0 4px #dff3e8; }
.status-dot.working { background: #b26a00; box-shadow: 0 0 0 4px #fbedd8; }
.status-dot.error { background: var(--danger); box-shadow: 0 0 0 4px #fde8e7; }
.sync-summary p { margin: 3px 0 0; font-size: 12.5px; }
.sync-facts { display: grid; gap: 9px; margin: 0 0 14px; }
.sync-facts div { display: flex; justify-content: space-between; gap: 16px; font-size: 12.5px; border-bottom: 1px solid #eef2f5; padding-bottom: 8px; }
.sync-facts dt { color: var(--muted); }
.sync-facts dd { margin: 0; font-weight: 650; color: var(--navy); text-align: right; word-break: break-all; max-width: 65%; }
.warning-box { background: #fff7e6; border: 1px solid #f0d9a8; border-radius: 10px; padding: 13px 15px; margin-bottom: 14px; }
.warning-box strong { color: #7a4c00; font-size: 13.5px; }
.warning-box p { margin: 5px 0 0; font-size: 12.5px; line-height: 1.5; color: #6b532c; }
.recovery-code {
  font-family: Consolas, "Courier New", monospace;
  font-size: clamp(15px, 3vw, 21px);
  font-weight: 700;
  letter-spacing: .08em;
  text-align: center;
  background: var(--navy);
  color: var(--white);
  padding: 18px 12px;
  border-radius: 10px;
  margin-bottom: 12px;
  word-break: break-all;
  user-select: all;
}
.check-line { display: flex; gap: 9px; align-items: flex-start; font-size: 13px; margin: 10px 0 14px; }
.check-line input { margin-top: 3px; }
.locked-box { background: #fff1f0; border: 1px solid #f3c9c6; border-radius: 10px; padding: 13px 15px; margin: 10px 0 14px; }
.locked-box strong { color: #8f1d14; font-size: 13.5px; }
.locked-box p { margin: 5px 0 10px; font-size: 12.5px; line-height: 1.5; color: #6b3a35; }
.locked-row { display: flex; gap: 8px; flex-wrap: wrap; }
.locked-row input { flex: 1 1 190px; border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; font-size: 13.5px; }

@media (max-width: 720px) {
  .app-header { padding: 8px 12px; }
  .local-pill { display: none; }
  .header-actions .btn.small { padding: 7px 10px; }
  .library-view { padding: 20px 14px; }
  .hero h1 { font-size: clamp(32px, 10vw, 44px); }
  .hero .lead { font-size: 14.5px; }
  .doc-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
  .doc-card { min-height: 150px; padding: 13px; }
  .viewer-bar { padding: 6px 8px; gap: 4px; }
  .doc-title { flex-basis: 100%; order: -1; padding-left: 2px; }
  .viewer-tools { width: 100%; justify-content: flex-start; }
  .divider { display: none; }
  .stage { padding: 12px; }
  .text-stage { padding: 10px; }
  #textEditor { min-height: calc(100svh - 260px); padding: 20px; }
  .app-footer { font-size: 10px; }
}

@media (max-width: 430px) {
  .viewer-tools .btn.small { padding: 6px 9px; font-size: 11.5px; }
  .icon-btn { width: 30px; height: 30px; }
  .page-pill input { width: 34px; }
}
