:root {
  --bg-app: #ecebe7;
  --bg-card: #f8f7f3;
  --bg-card-header: rgba(0, 0, 0, 0.03);
  --bg-input: rgba(0, 0, 0, 0.065);
  --border-subtle: rgba(0, 0, 0, 0.11);
  --text-primary: #1c1c1e;
  --text-secondary: #5c5c60;
  --text-tertiary: #8e8e93;
  --accent: #007aff;
  --orange: #e67e22;
  --orange-bg: rgba(230, 126, 34, 0.12);
  --orange-bg-strong: rgba(230, 126, 34, 0.22);
  --green: #16a34a;
  --red: #dc2626;
  --purple: #9333ea;
  --reader-font: Georgia, "New York", "Times New Roman", serif;
  --reader-size: 16px;
}

[data-theme="dark"] {
  --bg-app: #141416;
  --bg-card: #1d1d20;
  --bg-card-header: rgba(255, 255, 255, 0.035);
  --bg-input: rgba(255, 255, 255, 0.08);
  --border-subtle: rgba(255, 255, 255, 0.12);
  --text-primary: #f2f2f7;
  --text-secondary: #aeaeb2;
  --text-tertiary: #6e6e73;
  --accent: #38bdf8;
  --orange: #fb923c;
  --orange-bg: rgba(251, 146, 60, 0.14);
  --orange-bg-strong: rgba(251, 146, 60, 0.24);
}

[data-theme="light"] {
  --bg-app: #f3f4f6;
  --bg-card: #ffffff;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-app);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 1120px;
  margin: 0 auto;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.app-shell.is-home {
  justify-content: center;
}
.app-shell.is-home .search-header {
  width: 100%;
  max-width: 640px;
  margin: -14vh auto 0;
  padding: 10px 18px 8px;
}
.app-shell.is-home .search-capsule {
  padding: 10px 15px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}
.app-shell.is-home .main-stage,
.app-shell.is-home .collision-strip {
  display: none;
}
.app-shell.is-home .bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: env(safe-area-inset-bottom);
  max-width: 1120px;
  margin: 0 auto;
}

.sf-icon {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.85;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

.search-leading-icon {
  color: var(--text-tertiary);
  display: inline-flex;
  align-items: center;
}

.home-quick-bar {
  display: none;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px 0;
}
.app-shell.is-home .home-quick-bar {
  display: flex;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: 12px 18px 0;
}

@keyframes spinSlow {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
.spinning { animation: spinSlow 0.9s linear infinite; }

/* Top Search Bar */
.search-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 8px;
  width: 100%;
  flex-shrink: 0;
}

.back-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 16px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.12s ease, background 0.12s ease, opacity 0.12s ease;
}
.back-btn.hidden { display: none; }
.back-btn:hover:not(:disabled) { transform: scale(1.06); background: rgba(128,128,128,0.18); }
.back-btn:active:not(:disabled) { transform: scale(0.95); }
.back-btn:disabled { opacity: 0.32; cursor: default; }

.search-capsule {
  flex: 1;
  min-width: 0;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
}

.search-capsule input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 16px;
  min-width: 0;
}
.search-capsule input::placeholder { color: var(--text-tertiary); font-size: 14px; }

.icon-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 13px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: background 0.12s ease, transform 0.12s ease;
}
.icon-btn:hover:not(:disabled) { background: rgba(128,128,128,0.14); color: var(--text-primary); }
.icon-btn:active:not(:disabled) { transform: scale(0.96); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }

/* Collision Switcher Strip */
.collision-strip {
  display: flex;
  gap: 6px;
  padding: 0 14px 8px;
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none;
}
.collision-strip::-webkit-scrollbar { display: none; }
.collision-strip:empty { display: none; padding: 0; }

.pill {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 500;
  background: var(--bg-input);
  color: var(--text-secondary);
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.12s ease;
}
.pill:hover { background: rgba(128,128,128,0.18); transform: scale(1.02); }
.pill.active { background: rgba(0,122,255,0.18); color: var(--accent); border-color: rgba(0,122,255,0.35); font-weight: 600; }
.pill.orange { background: var(--orange-bg); color: var(--orange); border-color: rgba(230,126,34,0.32); font-weight: 600; }

/* Main Reader Card */
.main-stage {
  flex: 1;
  min-height: 0;
  padding: 0 12px;
  display: flex;
  gap: 10px;
}

.reader-card {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
}

.doc-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--bg-card-header);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.doc-title {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}
.badge.green { background: rgba(22,163,74,0.15); color: var(--green); }
.badge.orange { background: var(--orange-bg); color: var(--orange); }
.badge.red { background: rgba(220,38,38,0.15); color: var(--red); }
.badge.purple { background: rgba(147,51,234,0.15); color: var(--purple); }

.doc-spacer { flex: 1; }

/* Find Bar */
.find-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  background: var(--bg-card-header);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.find-bar.hidden { display: none; }
.find-bar input {
  flex: 1;
  border: 1px solid var(--border-subtle);
  border-radius: 7px;
  padding: 4px 8px;
  font-size: 13px;
  background: var(--bg-card);
  color: var(--text-primary);
  outline: none;
}
.find-count { font-size: 11.5px; color: var(--text-secondary); font-variant-numeric: tabular-nums; min-width: 48px; text-align: center; }

/* Reader Body + Index Split */
.reader-split {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
}

.reader-scroll {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 18px 20px 36px;
}

.sticky-lead {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-subtle);
  border-left: 3.5px solid var(--orange);
  padding: 9px 14px;
  margin: -18px -20px 14px;
  font-family: var(--reader-font);
  font-size: calc(var(--reader-size) - 1px);
  box-shadow: 0 3px 10px rgba(0,0,0,0.08);
  cursor: pointer;
}
.sticky-lead.hidden { display: none; }

.crumb { font-size: 11px; color: var(--text-tertiary); margin-bottom: 4px; }
.art-heading { font-family: var(--reader-font); font-size: calc(var(--reader-size) + 5px); font-weight: 700; margin: 0 0 4px; }
.art-epigrafe { font-family: var(--reader-font); font-size: var(--reader-size); font-style: italic; color: var(--text-secondary); margin: 0 0 14px; }

.para-block {
  font-family: var(--reader-font);
  font-size: var(--reader-size);
  line-height: 1.6;
  padding: 7px 10px;
  margin: 0 -10px 8px;
  border-radius: 8px;
  border-left: 3px solid transparent;
  cursor: pointer;
  transition: background 0.12s ease;
}
.para-block:hover { background: rgba(128,128,128,0.06); }
.para-block.focused { background: var(--orange-bg); border-left-color: var(--orange); }

.para-num {
  display: inline-block;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 11.5px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--bg-input);
  color: var(--text-secondary);
  margin-right: 6px;
  vertical-align: 1px;
}
.para-block.focused .para-num { background: var(--orange); color: #fff; }

.alinea-block {
  margin: 6px 0 4px 18px;
  padding: 5px 9px;
  border-radius: 7px;
  border-left: 2.5px solid transparent;
  cursor: pointer;
}
.alinea-block:hover { background: rgba(128,128,128,0.07); }
.alinea-block.focused { background: var(--orange-bg-strong); border-left-color: var(--orange); font-weight: 600; }

.subalinea-block {
  margin: 4px 0 3px 20px;
  padding: 3px 7px;
  border-radius: 6px;
}
.subalinea-block.focused { background: var(--orange-bg-strong); font-weight: 600; }

.inline-ref {
  border: none;
  background: none;
  padding: 0 2px;
  font: inherit;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}

mark.find-hit { background: rgba(250, 204, 21, 0.45); color: inherit; border-radius: 3px; padding: 0 1px; }
mark.find-hit.active { background: #f97316; color: #fff; }

/* Amendments Box */
.amendments-box {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--orange-bg);
  border: 1px solid rgba(230,126,34,0.28);
}
.amendments-title { font-size: 11px; font-weight: 700; letter-spacing: 0.4px; color: var(--orange); margin-bottom: 8px; }
.amendment-row {
  width: 100%;
  text-align: left;
  border: 1px solid var(--border-subtle);
  background: var(--bg-card);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
  cursor: pointer;
  display: block;
}
.amendment-row:hover { border-color: var(--orange); }

/* Index Sidebar / Mobile Drawer */
.index-sidebar {
  width: 265px;
  border-left: 1px solid var(--border-subtle);
  background: var(--bg-card-header);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.index-sidebar.hidden { display: none; }
.index-filter { padding: 8px; border-bottom: 1px solid var(--border-subtle); }
.index-filter input {
  width: 100%;
  border: 1px solid var(--border-subtle);
  border-radius: 7px;
  padding: 5px 8px;
  font-size: 12.5px;
  background: var(--bg-card);
  color: var(--text-primary);
  outline: none;
}
.index-list { flex: 1; overflow-y: auto; padding: 6px; }
.index-item {
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text-primary);
  padding: 7px 8px;
  border-radius: 7px;
  cursor: pointer;
  display: block;
  font-size: 12.5px;
}
.index-item:hover { background: var(--bg-input); }
.index-item.active { background: rgba(0,122,255,0.16); color: var(--accent); font-weight: 600; }
.index-epi { display: block; font-size: 11px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Bottom Toolbar */
.bottom-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  width: 100%;
  flex-shrink: 0;
}
.bottom-bar .icon-btn {
  white-space: nowrap;
  flex-shrink: 0;
}
.bottom-bar .icon-btn.copied {
  color: var(--green);
}
.bottom-spacer { flex: 1; min-width: 8px; }

/* Empty / Home State */
.empty-state {
  padding: 28px 20px;
  text-align: center;
  color: var(--text-secondary);
}
.empty-state h2 { font-family: var(--reader-font); color: var(--text-primary); margin: 0 0 8px; font-size: 22px; }
.quick-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 16px;
}

/* Settings Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop.hidden { display: none; }
.modal {
  width: 100%;
  max-width: 600px;
  max-height: 85dvh;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 18px 48px rgba(0,0,0,0.28);
}
.modal-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-subtle);
}
.modal-tabs { display: flex; gap: 4px; background: var(--bg-input); padding: 3px; border-radius: 8px; }
.modal-tab {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.modal-tab.active { background: var(--bg-card); color: var(--text-primary); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }
.modal-body { flex: 1; overflow-y: auto; padding: 14px 16px; }
.settings-group { border: 1px solid var(--border-subtle); border-radius: 10px; padding: 12px; margin-bottom: 12px; }
.settings-group h4 { margin: 0 0 8px; font-size: 12px; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.4px; }
.progress-track { height: 8px; border-radius: 999px; background: var(--bg-input); overflow: hidden; margin: 8px 0; }
.progress-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.2s ease; }

.note-box {
  margin-top: 12px;
  padding: 10px;
  border-radius: 9px;
  background: var(--orange-bg);
  border: 1px solid rgba(230,126,34,0.25);
}
.note-box textarea {
  width: 100%;
  border: 1px solid var(--border-subtle);
  border-radius: 7px;
  padding: 7px;
  font: inherit;
  font-size: 13px;
  background: var(--bg-card);
  color: var(--text-primary);
  resize: vertical;
  min-height: 54px;
}

@media (max-width: 740px) {
  .index-sidebar {
    position: absolute;
    inset: 0;
    width: 100%;
    z-index: 20;
    background: var(--bg-card);
  }
  .reader-scroll { padding: 14px 14px 32px; }
  .sticky-lead { margin: -14px -14px 12px; }
}

@media (max-width: 600px) {
  .bottom-bar .btn-label {
    display: none;
  }
  .bottom-bar {
    justify-content: space-between;
    gap: 2px;
    padding: 6px 12px;
  }
  .bottom-bar .bottom-spacer {
    display: none;
  }
  .bottom-bar .icon-btn {
    padding: 8px;
    min-width: 40px;
    min-height: 36px;
    justify-content: center;
    border-radius: 10px;
  }
  .bottom-bar .sf-icon {
    width: 18px;
    height: 18px;
  }
  .search-header {
    padding: 10px 10px 8px;
  }
  .app-shell.is-home .search-header {
    padding: 10px 14px 8px;
  }
  .main-stage {
    padding: 0 10px;
  }
}

@media (max-width: 460px) {
  .doc-header .btn-label {
    display: none;
  }
}

/* Portrait / vertical layout (iOS / iPadOS): reader card on top with extra top padding for iOS status-bar blur, controls docked at bottom */
@media (orientation: portrait) and (max-width: 900px) {
  .app-shell:not(.is-home) {
    padding-top: calc(env(safe-area-inset-top) + 18px);
  }
  .app-shell:not(.is-home) .main-stage {
    order: 1;
    padding: 6px 10px 6px;
  }
  .app-shell:not(.is-home) .collision-strip {
    order: 2;
    padding: 4px 12px 6px;
  }
  .app-shell:not(.is-home) .collision-strip:empty {
    display: none;
    padding: 0;
  }
  .app-shell:not(.is-home) .search-header {
    order: 3;
    padding: 4px 10px 4px;
  }
  .app-shell:not(.is-home) .bottom-bar {
    order: 4;
    padding: 4px 12px 6px;
  }
}
