:root {
  --cream: #f5f0e8;
  --paper: #faf7f2;
  --ink: #1a1208;
  --ink-soft: #4a3f2f;
  --ink-faint: #9a8e7e;
  --amber: #c8832a;
  --amber-dark: #a06820;
  --amber-pale: #f5e6cc;
  --rust: #a84632;
  --sage: #4a7c59;
  --shadow: rgba(26,18,8,0.12);
  --border: rgba(26,18,8,0.12);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--cream);
  color: var(--ink);
  min-height: 100vh;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  overflow-x: hidden;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse at 80% 10%, rgba(200,131,42,0.08) 0%, transparent 60%),
    radial-gradient(ellipse at 20% 90%, rgba(74,124,89,0.06) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

.view { display: none; min-height: 100vh; position: relative; z-index: 1; }
.view.active { display: flex; flex-direction: column; }

/* ── HEADER ── */
.app-header {
  padding: 28px 24px 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(245,240,232,0.9);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.header-top { display: flex; align-items: center; justify-content: space-between; }
.logo { font-family: 'Playfair Display', serif; font-size: 30px; font-weight: 700; color: var(--ink); letter-spacing: -0.5px; }
.logo span { color: var(--amber); }
.header-controls { display: flex; align-items: center; gap: 12px; }
.help-btn { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--paper); color: var(--ink-soft); font-size: 16px; font-weight: 700; cursor: pointer; transition: all 0.15s; }
.help-btn:active { background: var(--amber-pale); border-color: var(--amber); color: var(--amber-dark); }
.theme-btn { font-size: 15px; line-height: 1; }
.lang-switch { display: flex; gap: 4px; background: var(--paper); border-radius: 8px; padding: 4px; border: 1px solid var(--border); }
.lang-btn { background: none; border: none; padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 14px; transition: all 0.15s; color: var(--ink-faint); }
.lang-btn.active { background: var(--amber); color: white; }
.sync-indicator { font-size: 12px; color: var(--ink-faint); min-width: 40px; text-align: right; }
.subtitle { font-size: 11px; color: var(--ink-faint); letter-spacing: 1.5px; text-transform: uppercase; margin-top: 2px; }

/* ── HOME ── */
.stories-list { flex: 1; padding: 20px 20px 100px; display: flex; flex-direction: column; gap: 12px; }

.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 60px 32px; text-align: center; gap: 16px; }
.empty-icon { font-size: 56px; opacity: 0.35; }
.empty-state h2 { font-family: 'Playfair Display', serif; font-size: 22px; color: var(--ink); opacity: 0.7; }
.empty-state p { font-size: 14px; color: var(--ink-faint); line-height: 1.6; max-width: 240px; }

.story-card {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 14px;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px var(--shadow);
  position: relative;
  overflow: hidden;
  animation: cardIn 0.35s ease both;
}
.story-card::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--amber); border-radius: 14px 0 0 14px; }
.story-card:active { transform: scale(0.97); box-shadow: 0 1px 4px var(--shadow); }
.story-card-icon { font-size: 26px; flex-shrink: 0; }
.story-card-body { flex: 1; min-width: 0; }
.story-card-title { font-family: 'Playfair Display', serif; font-size: 17px; font-weight: 700; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.story-card-meta { font-size: 12px; color: var(--ink-faint); margin-top: 3px; }

/* ── NEW: metadata tags ── */
.story-card-tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }
.meta-tag {
  font-size: 10px; font-weight: 600;
  padding: 2px 8px; border-radius: 20px;
  background: var(--amber-pale); color: var(--amber-dark);
  border: 1px solid rgba(200,131,42,0.25);
  letter-spacing: 0.2px;
}
.meta-tag.genre { background: rgba(74,124,89,0.1); color: var(--sage); border-color: rgba(74,124,89,0.25); }
.meta-tag.lang  { background: rgba(26,18,8,0.06); color: var(--ink-soft); border-color: var(--border); }

.story-card-progress { width: 38px; height: 38px; border-radius: 50%; border: 2px solid var(--border); display: flex; align-items: center; justify-content: center; font-size: 10px; color: var(--ink-faint); font-weight: 600; flex-shrink: 0; background: var(--cream); }
.story-card-progress.active { border-color: var(--amber); color: var(--amber); background: var(--amber-pale); }
.story-card-delete { width: 32px; height: 32px; border-radius: 50%; border: none; background: transparent; color: var(--ink-faint); font-size: 16px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s; flex-shrink: 0; }
.story-card-delete:active { background: rgba(168,70,50,0.12); color: var(--rust); }

.fab { position: fixed; bottom: 76px; right: 24px; width: 58px; height: 58px; border-radius: 50%; background: var(--amber); color: white; border: none; font-size: 28px; cursor: pointer; box-shadow: 0 4px 20px rgba(200,131,42,0.45); display: flex; align-items: center; justify-content: center; transition: all 0.2s ease; z-index: 20; }
.fab:active { transform: scale(0.92); background: var(--amber-dark); }

/* ── EDITOR ── */
.editor-header { display: flex; align-items: center; gap: 12px; padding: 20px 16px 14px; border-bottom: 1px solid var(--border); background: rgba(245,240,232,0.9); backdrop-filter: blur(8px); position: sticky; top: 0; z-index: 10; }
.back-btn { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--ink); width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 8px; transition: background 0.15s; }
.back-btn:hover { background: rgba(26,18,8,0.07); }
.editor-header h2 { font-family: 'Playfair Display', serif; font-size: 20px; flex: 1; }
.editor-body { flex: 1; padding: 20px; display: flex; flex-direction: column; gap: 20px; }

.guide-card { background: var(--paper); border: 1px solid var(--border); border-radius: 14px; padding: 20px; box-shadow: 0 2px 8px var(--shadow); }
.guide-card h3 { font-family: 'Playfair Display', serif; font-size: 16px; margin-bottom: 10px; color: var(--amber-dark); }
.guide-card p { font-size: 13px; color: var(--ink-soft); line-height: 1.7; margin-bottom: 8px; }
.guide-card p:last-child { margin-bottom: 0; }
.guide-card a { color: var(--amber); text-decoration: none; font-weight: 600; }
.guide-card .editor-cta-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: 10px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--amber-dark);
  background: var(--amber);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(200,131,42,0.3);
  transition: background 0.2s ease, transform 0.2s ease;
}
.guide-card .editor-cta-btn:hover {
  background: var(--amber-dark);
  transform: translateY(-1px);
}
.guide-card code { font-family: monospace; background: var(--cream); padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.json-example { background: #1a1208; color: #e8d5b0; border-radius: 10px; padding: 14px; font-family: 'Courier New', monospace; font-size: 11.5px; line-height: 1.6; overflow-x: auto; white-space: pre; margin-top: 12px; }
.jk { color: #f4a261; }
.js { color: #a8e6cf; }
.jn { color: #c8e6ff; }

.section-title { font-size: 11px; text-transform: uppercase; letter-spacing: 2px; color: var(--ink-faint); font-weight: 600; padding: 4px 0; }

.import-tabs { display: flex; gap: 8px; }
.tab-btn { flex: 1; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border); background: transparent; color: var(--ink-faint); font-size: 13px; font-family: 'DM Sans', sans-serif; cursor: pointer; font-weight: 500; transition: all 0.15s; }
.tab-btn.active { background: var(--amber); color: white; border-color: var(--amber); }

textarea.json-input { width: 100%; min-height: 200px; padding: 14px; background: var(--paper); border: 1px solid var(--border); border-radius: 12px; font-family: 'Courier New', monospace; font-size: 12px; color: var(--ink); line-height: 1.6; resize: vertical; outline: none; transition: border-color 0.2s; }
textarea.json-input:focus { border-color: var(--amber); box-shadow: 0 0 0 3px rgba(200,131,42,0.1); }

.file-drop-zone { border: 2px dashed var(--border); border-radius: 12px; padding: 32px 20px; text-align: center; cursor: pointer; transition: all 0.2s; background: var(--paper); display: none; }
.file-drop-zone.shown { display: block; }
.file-drop-zone.dragover { border-color: var(--amber); background: var(--amber-pale); }
.file-drop-zone p { font-size: 13px; color: var(--ink-faint); margin-top: 8px; }
.file-drop-zone .drop-icon { font-size: 36px; }
#file-input { display: none; }

.error-msg { font-size: 12px; color: var(--rust); padding: 10px 14px; background: rgba(168,70,50,0.08); border-radius: 8px; border: 1px solid rgba(168,70,50,0.2); display: none; }
.error-msg.visible { display: block; }

.btn-primary { width: 100%; padding: 16px; border-radius: 14px; border: none; background: var(--amber); color: white; font-size: 15px; font-family: 'DM Sans', sans-serif; font-weight: 600; cursor: pointer; transition: all 0.2s; box-shadow: 0 4px 16px rgba(200,131,42,0.35); }
.btn-primary:hover { background: var(--amber-dark); }
.btn-primary:active { transform: scale(0.97); }

/* ── READER ── */
#reader { background: var(--paper); }
.reader-header { display: flex; align-items: center; padding: 18px 14px 12px; border-bottom: 1px solid var(--border); background: rgba(250,247,242,0.92); backdrop-filter: blur(8px); position: sticky; top: 0; z-index: 10; gap: 8px; }
.reader-title { font-family: 'Playfair Display', serif; font-size: 15px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page-badge { font-size: 11px; color: var(--ink-faint); background: var(--cream); padding: 4px 10px; border-radius: 20px; border: 1px solid var(--border); white-space: nowrap; flex-shrink: 0; }
.icon-btn { background: none; border: 1px solid var(--border); color: var(--ink-soft); width: 34px; height: 34px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; cursor: pointer; transition: all 0.15s; flex-shrink: 0; font-family: monospace; letter-spacing: -1px; }
.icon-btn:active { background: var(--cream); border-color: var(--amber); color: var(--amber); }

.progress-bar-wrap { height: 3px; background: var(--amber-pale); }
.progress-bar-fill { height: 100%; background: linear-gradient(90deg, var(--amber), var(--amber-dark)); transition: width 0.4s ease; }

.reader-body { flex: 1; padding: 28px 24px 40px; display: flex; flex-direction: column; gap: 28px; }
.page-text { font-family: 'Playfair Display', serif; font-size: 18px; line-height: 1.9; color: var(--ink); animation: fadeSlide 0.4s ease both; }

.choices-section { display: flex; flex-direction: column; gap: 10px; animation: fadeSlide 0.4s 0.1s ease both; }
.choices-label { font-size: 11px; text-transform: uppercase; letter-spacing: 2px; color: var(--ink-faint); margin-bottom: 4px; }

.choice-btn { padding: 16px 20px; border-radius: 14px; border: 1.5px solid var(--border); background: var(--paper); color: var(--ink); font-family: 'DM Sans', sans-serif; font-size: 14px; font-weight: 500; text-align: left; cursor: pointer; transition: all 0.2s ease; display: flex; align-items: center; gap: 12px; box-shadow: 0 1px 4px var(--shadow); }
.choice-btn::before { content: '→'; color: var(--amber); font-size: 16px; flex-shrink: 0; transition: transform 0.2s; }
.choice-btn:active { border-color: var(--amber); background: var(--amber-pale); }
.choice-btn:active::before { transform: translateX(3px); }

.end-btn { padding: 18px; border-radius: 14px; border: none; background: var(--sage); color: white; font-family: 'DM Sans', sans-serif; font-size: 15px; font-weight: 600; cursor: pointer; transition: all 0.2s; text-align: center; box-shadow: 0 4px 16px rgba(74,124,89,0.3); }
.end-btn:active { transform: scale(0.97); }

.restart-btn { padding: 14px; border-radius: 14px; border: 1.5px solid var(--border); background: transparent; color: var(--ink-soft); font-family: 'DM Sans', sans-serif; font-size: 14px; font-weight: 500; cursor: pointer; transition: all 0.2s; text-align: center; }
.restart-btn:active { border-color: var(--amber); color: var(--amber); }

.completion-screen { display: none; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 48px 32px; gap: 16px; flex: 1; animation: fadeSlide 0.5s ease both; }
.completion-screen.visible { display: flex; }
.completion-icon { font-size: 68px; }
.completion-screen h2 { font-family: 'Playfair Display', serif; font-size: 24px; }
.completion-screen p { font-size: 14px; color: var(--ink-faint); line-height: 1.6; max-width: 260px; }
.completion-btns { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 300px; margin-top: 8px; }

/* ── MODAL ── */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(26,18,8,0.55); backdrop-filter: blur(4px); z-index: 100; padding: 20px; align-items: flex-end; justify-content: center; }
.modal-overlay.visible { display: flex; }
.modal-card { background: var(--paper); border-radius: 20px 20px 0 0; width: 100%; max-width: 480px; padding: 20px; max-height: 75vh; display: flex; flex-direction: column; gap: 14px; animation: slideUp 0.3s ease both; }
.modal-handle { width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: 0 auto -4px; }
.modal-title { font-family: 'Playfair Display', serif; font-size: 18px; text-align: center; }
.modal-json { flex: 1; background: var(--ink); border-radius: 12px; padding: 14px; font-family: 'Courier New', monospace; font-size: 11px; color: #e8d5b0; overflow: auto; white-space: pre; line-height: 1.6; max-height: 45vh; }
.modal-actions { display: flex; gap: 10px; }
.btn-secondary { flex: 1; padding: 14px; border-radius: 12px; border: 1.5px solid var(--border); background: transparent; color: var(--ink); font-family: 'DM Sans', sans-serif; font-size: 14px; font-weight: 500; cursor: pointer; transition: background 0.15s; }
.btn-secondary:active { background: var(--cream); }
.btn-copy { flex: 1; padding: 14px; border-radius: 12px; border: none; background: var(--amber); color: white; font-family: 'DM Sans', sans-serif; font-size: 14px; font-weight: 600; cursor: pointer; transition: background 0.15s; }
.btn-copy:active { background: var(--amber-dark); }

/* ── FOOTER ── */
.app-footer { text-align: center; padding: 14px 20px; font-size: 11px; color: var(--ink-faint); border-top: 1px solid var(--border); background: var(--cream); letter-spacing: 0.3px; }
.app-footer a { color: var(--amber); text-decoration: none; }

/* ── ANIMATIONS ── */
@keyframes fadeSlide { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
@keyframes cardIn    { from { opacity: 0; transform: translateY(8px);  } to { opacity: 1; transform: translateY(0); } }
@keyframes slideUp   { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* ── TOAST ── */
.toast { position: fixed; bottom: 84px; left: 50%; transform: translateX(-50%) translateY(16px); background: var(--ink); color: white; padding: 10px 22px; border-radius: 30px; font-size: 13px; font-weight: 500; opacity: 0; transition: all 0.3s ease; z-index: 200; white-space: nowrap; pointer-events: none; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }


/* ── TUTORIAL ── */
.tutorial-page { min-height: 100vh; display: flex; flex-direction: column; position: relative; z-index: 1; }
.tutorial-header { padding: 26px 24px 14px; border-bottom: 1px solid var(--border); }
.tutorial-header h1 { font-family: 'Playfair Display', serif; font-size: 30px; }
.tutorial-header p { margin-top: 6px; color: var(--ink-faint); font-size: 13px; }
.tutorial-content { flex: 1; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.tutorial-step { background: var(--paper); border: 1px solid var(--border); border-radius: 14px; padding: 16px; box-shadow: 0 2px 8px var(--shadow); }
.tutorial-step h3 { font-size: 15px; margin-bottom: 8px; color: var(--amber-dark); }
.tutorial-step p { font-size: 14px; line-height: 1.6; color: var(--ink-soft); }
.tutorial-actions { padding: 0 20px 20px; display: flex; flex-direction: column; gap: 10px; }

/* ── AUTH + SETTINGS PAGES ── */
.auth-page,
.settings-page {
  min-height: 100vh;
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 18px 40px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-header h1,
.settings-header h1 {
  font-family: 'Playfair Display', serif;
  font-size: 32px;
  color: var(--ink);
}

.auth-header p,
.settings-header p {
  margin-top: 8px;
  color: var(--ink-faint);
  font-size: 14px;
}

.back-link {
  display: inline-flex;
  margin-bottom: 10px;
  text-decoration: none;
  color: var(--amber-dark);
  font-weight: 600;
}

.auth-card,
.settings-card {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 2px 8px var(--shadow);
}

.auth-card h2,
.settings-card h2 {
  font-size: 18px;
  margin-bottom: 12px;
  color: var(--amber-dark);
}

.auth-form,
.settings-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.auth-form input {
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 12px;
  font-family: 'DM Sans', sans-serif;
  background: var(--cream);
  color: var(--ink);
}

.auth-actions,
.settings-actions.inline {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.auth-link,
.settings-btn {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--ink);
  text-decoration: none;
  font-family: 'DM Sans', sans-serif;
  font-size: 14px;
  text-align: center;
  cursor: pointer;
}

.status-msg {
  min-height: 1.2rem;
  font-size: 14px;
  color: #2d8b45;
}

.status-msg.error {
  color: #c73b45;
}

.muted {
  color: var(--ink-faint);
}

.settings-lang-btn.active {
  background: var(--amber);
  color: #fff;
  border-color: var(--amber);
}

.warning-card {
  border-color: #c73b45;
  background: #fff4f4;
}

.warning-card h2 {
  color: #9f1f28;
  margin-bottom: 6px;
}

.warning-card p {
  color: #742029;
  font-size: 14px;
  line-height: 1.45;
}
