:root {
  --bg: #f6f3ee;
  --panel: #ffffff;
  --ink: #241c17;
  --muted: #6d625a;
  --line: #e2dbd1;
  --brand: #5a4f2f;
  --brand-dark: #2c221c;
  --accent: #8a7a4c;
  --focus: #b8932f;
  --danger: #a3301f;
  --ok: #2f6b3a;
  --sans: "Segoe UI", -apple-system, BlinkMacSystemFont, Helvetica, Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #18130f;
    --panel: #221b16;
    --ink: #f1e9dc;
    --muted: #b3a697;
    --line: #3a2f27;
    --brand: #8a7a4c;
    --accent: #c2ae76;
    --danger: #e0715f;
    --ok: #7cc28a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.5 var(--sans); }
a { color: inherit; }
button { font: inherit; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.btn-primary, .btn-secondary, .btn-ghost {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 6px; padding: 9px 16px; cursor: pointer; text-decoration: none; font-weight: 600; font-size: 14px;
  border: 1px solid transparent; transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--brand-dark); }
.btn-primary:disabled { opacity: 0.45; cursor: default; }
.btn-secondary { background: var(--panel); color: var(--ink); border-color: var(--line); }
.btn-secondary:hover { border-color: var(--accent); }
.btn-ghost { background: transparent; color: inherit; border-color: rgba(255, 255, 255, 0.28); }
.btn-ghost:hover { border-color: rgba(255, 255, 255, 0.7); }

/* ---------- Login ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: #2c221c; }
.login-card { width: 100%; max-width: 380px; background: #fff; color: #241c17; border-radius: 10px; padding: 36px 32px 28px; box-shadow: 0 20px 50px rgba(0,0,0,.35); border-top: 6px solid #5a4f2f; }
.login-logo { font-family: "Cinzel", Georgia, serif; font-weight: 700; font-size: 28px; text-decoration: none; color: #2c221c; }
.login-card h1 { font-size: 20px; margin: 18px 0 2px; }
.login-sub { color: #6d625a; margin: 0 0 20px; }
.login-form { display: grid; gap: 6px; }
.login-form label { font-weight: 600; font-size: 14px; margin-top: 8px; }
.login-form input { width: 100%; padding: 10px 12px; border: 1px solid #cfc6ba; border-radius: 6px; font: inherit; background: #fff; color: #241c17; }
.login-form .btn-primary { margin-top: 18px; padding: 11px; background: #5a4f2f; }
.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 64px; }
.pw-toggle { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); border: 0; background: none; color: #5a4f2f; font-weight: 600; cursor: pointer; padding: 6px 8px; }
.alert { background: #fbeae6; color: #8a2414; border: 1px solid #f1c6bd; border-radius: 6px; padding: 10px 12px; font-size: 14px; }
.back-link { display: inline-block; margin-top: 18px; font-size: 13px; color: #6d625a; }

/* ---------- Editor shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  background: #2c221c; color: #f3ead8; padding: 10px 20px; border-bottom: 4px solid #5a4f2f;
}
.topbar-title { display: flex; align-items: baseline; gap: 12px; }
.topbar-title strong { font-family: "Cinzel", Georgia, serif; font-size: 18px; letter-spacing: .02em; }
.topbar-title span { color: #c9bda5; font-size: 14px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.topbar-actions form { margin: 0; }
.topbar .btn-ghost { color: #f3ead8; padding: 7px 12px; }

.layout { display: grid; grid-template-columns: 230px 1fr; max-width: 1180px; margin: 0 auto; padding: 24px 20px 120px; gap: 28px; }
.sidebar { position: sticky; top: 80px; align-self: start; display: flex; flex-direction: column; gap: 2px; }
.sidebar a { display: flex; align-items: center; justify-content: space-between; padding: 9px 12px; border-radius: 6px; text-decoration: none; color: var(--muted); font-weight: 500; }
.sidebar a:hover { background: var(--panel); color: var(--ink); }
.sidebar a.active { background: var(--panel); color: var(--ink); font-weight: 650; box-shadow: inset 3px 0 0 var(--brand); }
.sidebar a.has-changes::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--focus); }
.sidebar-history { margin-top: 14px; border-top: 1px solid var(--line); border-radius: 0 !important; padding-top: 14px !important; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 28px 32px 32px; min-width: 0; }
.panel-head h1 { margin: 0 0 4px; font-size: 22px; }
.note { color: var(--muted); margin: 0 0 8px; max-width: 640px; }
.panel-head { margin-bottom: 18px; }

.field { padding: 16px 0; border-top: 1px solid var(--line); }
.field.is-dirty .field-label::after { content: "Edited"; margin-left: 8px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--focus); }
.field-label { display: block; font-weight: 650; margin-bottom: 8px; }
.input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; font: inherit; background: var(--bg); color: var(--ink); resize: vertical; }
.input:focus { border-color: var(--accent); background: var(--panel); outline: none; box-shadow: 0 0 0 3px rgba(184, 147, 47, 0.2); }

.group { border: 1px solid var(--line); border-radius: 8px; padding: 4px 18px 6px; margin: 18px 0 6px; }
.group legend { font-weight: 700; padding: 0 6px; color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .06em; }
.group .field:first-of-type { border-top: 0; }

/* Rich text */
.rich { border: 1px solid var(--line); border-radius: 6px; background: var(--panel); overflow: clip; }
.rich:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(184, 147, 47, 0.2); }
.toolbar { display: flex; flex-wrap: wrap; gap: 2px; padding: 6px; background: var(--bg); border-bottom: 1px solid var(--line); position: sticky; top: 62px; z-index: 2; }
.toolbar button { border: 1px solid transparent; background: transparent; color: var(--ink); border-radius: 4px; padding: 4px 9px; cursor: pointer; font-size: 13px; min-width: 32px; }
.toolbar button:hover { background: var(--panel); border-color: var(--line); }
.toolbar .sep { width: 1px; background: var(--line); margin: 3px 4px; }
.editable { min-height: 120px; max-height: 70vh; overflow: auto; padding: 14px 16px; outline: none; font-family: "Times New Roman", Times, Georgia, serif; font-size: 17px; line-height: 1.6; }
.rich-inline .editable { min-height: 0; font-style: italic; }
.editable p { margin: 0 0 .8em; }
.editable h2, .editable h3 { font-size: 18px; margin: 1em 0 .4em; }
.editable a { color: var(--brand); text-decoration: underline; }
.editable ul, .editable ol { padding-left: 1.4em; }

/* Images */
.image-field { display: flex; gap: 18px; align-items: flex-start; }
.thumb { width: 180px; height: 130px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); flex-shrink: 0; }
.file-btn { cursor: pointer; position: relative; }
.file-btn:focus-within { outline: 2px solid var(--focus); outline-offset: 2px; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.upload-status { font-size: 13px; margin: 6px 0 0; color: var(--ok); min-height: 1em; }
.upload-status.error { color: var(--danger); }

/* History */
.history { list-style: none; margin: 0; padding: 0; }
.history li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.history li.empty { color: var(--muted); }

/* Save bar */
.savebar {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 20;
  display: flex; align-items: center; gap: 12px;
  background: #2c221c; color: #f3ead8; padding: 10px 10px 10px 18px; border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,.25); max-width: calc(100vw - 32px);
}
.savebar .btn-ghost { color: #f3ead8; }
.savebar .btn-primary { background: #8a7a4c; }
.savebar .btn-primary:hover:not(:disabled) { background: #a08e58; }
.savebar[data-state="dirty"] .save-msg { color: #f5d98c; }
.savebar[data-state="saved"] .save-msg { color: #9bd9a6; }
.savebar[data-state="error"] .save-msg { color: #ffb4a6; }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; padding: 12px 12px 120px; gap: 12px; }
  .sidebar { position: static; flex-direction: row; overflow-x: auto; gap: 4px; padding-bottom: 4px; }
  .sidebar a { white-space: nowrap; }
  .sidebar-history { margin-top: 0; border-top: 0; padding-top: 9px !important; }
  .panel { padding: 20px 16px; }
  .topbar-title span { display: none; }
  .image-field { flex-direction: column; }
  .thumb { width: 100%; height: 180px; }
  .toolbar { top: 56px; }
  .savebar { left: 12px; right: 12px; transform: none; justify-content: space-between; }
}
