@import url("https://fonts.googleapis.com/css2?family=Frank+Ruhl+Libre:wght@400;500;700&family=Assistant:wght@400;600;700&family=Noto+Rashi+Hebrew:wght@400;600&family=Noto+Serif+Hebrew:wght@400;600&display=swap");

:root {
  --bg-0: #f5f0e4;
  --bg-1: #f2dfc7;
  --bg-2: #d9e7f0;
  --paper: #fffdf8;
  --paper-strong: #ffffff;
  --ink: #1f2430;
  --muted: #5f6776;
  --brand: #a4472f;
  --brand-2: #1f5f7a;
  --line: #dfd3bd;
  --shadow: 0 14px 40px rgba(57, 41, 23, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "Assistant", "Arial", sans-serif;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background:
    radial-gradient(1200px 500px at 12% -8%, #f8d7a6 0%, transparent 58%),
    radial-gradient(1000px 450px at 84% -6%, #b8d5e8 0%, transparent 56%),
    linear-gradient(160deg, var(--bg-0) 0%, #f8f5ee 44%, #f4f0e6 100%);
}

.topbar {
  margin: 16px 16px 0;
  padding: 18px 22px;
  border: 1px solid rgba(223, 211, 189, 0.85);
  border-radius: 18px;
  background: linear-gradient(110deg, rgba(255, 252, 246, 0.93) 0%, rgba(247, 239, 225, 0.86) 58%, rgba(236, 244, 248, 0.9) 100%);
  box-shadow: var(--shadow);
}

.topbar h1 {
  margin: 0;
  font-family: "Frank Ruhl Libre", serif;
  font-size: 2rem;
  line-height: 1.1;
}

.topbar p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.98rem;
}

.layout {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  grid-template-rows: repeat(12, minmax(0, 1fr));
  gap: 12px;
  padding: 14px 16px 16px;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

body.genizah-connected .layout {
  grid-template-rows: auto repeat(11, minmax(0, 1fr));
}

.panel {
  background: linear-gradient(180deg, var(--paper-strong) 0%, var(--paper) 100%);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px;
  box-shadow: 0 8px 26px rgba(74, 51, 25, 0.09);
}

.panel-widget {
  position: relative;
  min-height: 0;
  overflow: hidden;
}

.panel h2 {
  margin: 0 0 8px;
  font-family: "Frank Ruhl Libre", serif;
  font-size: 1.45rem;
  line-height: 1.12;
  letter-spacing: 0.2px;
}

.controls-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.controls-panel.connected-compact {
  padding-top: 6px;
  padding-bottom: 6px;
}

.controls-panel.connected-compact .controls {
  gap: 8px;
}

.controls-panel.connected-compact select,
.controls-panel.connected-compact input,
.controls-panel.connected-compact button {
  padding-top: 6px;
  padding-bottom: 6px;
}

.commentary-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.text-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.halakha-panel,
.tanakh-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.genizah-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.genizah-panel .scroll-region {
  flex: 1;
  min-height: 0;
  height: auto;
}

.controls-panel {
  background: linear-gradient(100deg, #fff8ec 0%, #f6efe2 55%, #eef6fa 100%);
}

.panel-title-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
}

.panel-title-row a {
  color: var(--brand-2);
  text-decoration: none;
  font-weight: 700;
}

.panel-title-row a:hover {
  text-decoration: underline;
}

.text-panel .panel-title-row,
.text-panel #textMeta {
  display: none;
}

.controls {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: nowrap;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.layout-reset-btn {
  background: linear-gradient(120deg, #2f687f 0%, #4f8399 100%);
}

.layout-edit-toggle-btn {
  background: linear-gradient(120deg, #3f6e32 0%, #5d8e4d 100%);
}

.layout-save-btn {
  background: linear-gradient(120deg, #2d7f61 0%, #44a07c 100%);
}

.genizah-mode-btn {
  background: linear-gradient(120deg, #7b5a1b 0%, #9f7a33 100%);
}

.genizah-mode-btn.active {
  background: linear-gradient(120deg, #1f5f7a 0%, #387e9b 100%);
}

.settings-btn {
  background: linear-gradient(120deg, #3a4f86 0%, #5874b0 100%);
}

.site-logo {
  width: 42px;
  height: 42px;
  object-fit: contain;
  border-radius: 10px;
  border: 1px solid rgba(31, 95, 122, 0.22);
  background: #ffffff;
  padding: 3px;
  flex: 0 0 auto;
}

.controls-credit {
  margin-top: 8px;
  text-align: right;
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.4;
  white-space: normal;
}

.controls-credit a {
  color: var(--brand-2);
  text-decoration: underline;
}

@media (max-width: 1280px) {
  .controls-credit {
    margin-top: 8px;
  }
}

@media (max-width: 860px) {
  select,
  input,
  button {
    font-size: 0.88rem;
    padding: 6px 10px;
  }

  select,
  input {
    min-width: 120px;
  }

  .controls {
    gap: 8px;
  }

  .controls-credit {
    font-size: 0.82rem;
  }
}

.panel-drag-handle,
.panel-resize-handle,
.panel-resize-y-handle,
.panel-resize-x-handle {
  position: absolute;
  z-index: 6;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
}

.panel-widget[hidden] {
  display: none !important;
}

.settings-modal {
  position: fixed;
  inset: 0;
  z-index: 1200;
}

.settings-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(13, 19, 30, 0.42);
}

.settings-modal-panel {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  border: 1px solid #d8cab0;
  border-radius: 16px;
  background: #fffdf8;
  box-shadow: 0 24px 55px rgba(0, 0, 0, 0.25);
  padding: 14px;
}

.settings-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.settings-modal-header h3 {
  margin: 0;
  font-family: "Frank Ruhl Libre", serif;
  font-size: 1.45rem;
}

.settings-close-btn {
  width: 34px;
  height: 34px;
  border: 1px solid #d8cab0;
  border-radius: 10px;
  background: #fff;
  color: #27303d;
  font-size: 1.4rem;
  line-height: 1;
}

.settings-options {
  display: grid;
  gap: 8px;
}

.settings-options label {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid #e4d9c7;
  border-radius: 12px;
  background: #fff;
  padding: 9px 10px;
  font-weight: 700;
  color: #2f3744;
}

.settings-options input[type="checkbox"] {
  min-width: 18px;
  width: 18px;
  height: 18px;
  accent-color: #295f78;
}

.settings-actions {
  margin-top: 12px;
  display: flex;
  justify-content: flex-start;
}

.settings-apply-btn {
  background: linear-gradient(120deg, #2d7f61 0%, #44a07c 100%);
}

body.layout-edit-mode .panel-widget .panel-drag-handle,
body.layout-edit-mode .panel-widget .panel-resize-handle,
body.layout-edit-mode .panel-widget .panel-resize-y-handle,
body.layout-edit-mode .panel-widget .panel-resize-x-handle {
  opacity: 1;
  pointer-events: auto;
}

.panel-drag-handle {
  top: 6px;
  left: 8px;
  width: 44px;
  height: 18px;
  border-radius: 999px;
  background: rgba(31, 95, 122, 0.18);
  border: 1px solid rgba(31, 95, 122, 0.35);
  cursor: grab;
}

.panel-drag-handle::before {
  content: "";
  position: absolute;
  inset: 6px 10px;
  border-top: 2px solid rgba(31, 95, 122, 0.72);
  border-bottom: 2px solid rgba(31, 95, 122, 0.72);
}

.panel-resize-handle {
  left: 8px;
  bottom: 8px;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background:
    linear-gradient(135deg, transparent 0 33%, rgba(31, 95, 122, 0.65) 33% 42%, transparent 42% 58%, rgba(31, 95, 122, 0.65) 58% 67%, transparent 67% 100%);
  border: 1px solid rgba(31, 95, 122, 0.45);
  cursor: nwse-resize;
}

.panel-resize-y-handle {
  left: 28px;
  right: 28px;
  bottom: 8px;
  height: 8px;
  border-radius: 999px;
  border: 1px solid rgba(31, 95, 122, 0.35);
  background: rgba(31, 95, 122, 0.18);
  cursor: ns-resize;
}

.panel-resize-x-handle {
  left: 8px;
  top: 28px;
  bottom: 28px;
  width: 8px;
  border-radius: 999px;
  border: 1px solid rgba(31, 95, 122, 0.35);
  background: rgba(31, 95, 122, 0.18);
  cursor: ew-resize;
}

body.layout-dragging {
  user-select: none;
  cursor: grabbing;
}

.controls label {
  font-weight: 700;
  color: #3f433f;
}

select,
input,
button {
  font-family: "Assistant", sans-serif;
  font-size: 0.95rem;
  border-radius: 12px;
  padding: 8px 12px;
}

select,
input {
  min-width: 168px;
  border: 1px solid #d8cab0;
  background: #fffef9;
  color: #232734;
}

#tractateSelect,
#dafSelect {
  min-width: 0;
  width: auto;
  flex: 0 0 auto;
}

button {
  border: 1px solid transparent;
  background: linear-gradient(120deg, var(--brand) 0%, #b05c3c 100%);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(164, 71, 47, 0.24);
}

button:hover {
  filter: brightness(0.96);
}

.genizah-auth-form {
  margin-top: 10px;
  border-top: 1px dashed #d7c8ae;
  padding-top: 10px;
}

.genizah-credentials {
  display: grid;
  grid-template-columns: auto minmax(140px, 1fr) auto minmax(140px, 1fr) auto;
  gap: 8px 10px;
  align-items: center;
}

.genizah-credentials label {
  white-space: nowrap;
}

.genizah-credentials input {
  min-width: 0;
  width: 100%;
}

.genizah-credentials button {
  justify-self: start;
}

.genizah-auth-form.connected .genizah-credentials {
  display: none;
}

.genizah-auth-form.connected .auth-label {
  display: none;
}

.genizah-auth-form.connected {
  display: none;
}

.auth-label {
  font-weight: 700;
  color: #294c5f;
}

.auth-status {
  font-weight: 700;
  color: var(--muted);
}

.auth-status.connected {
  color: #1f5f7a;
}

.status,
.text-meta {
  margin: 8px 0 0;
  color: var(--muted);
}

.text-panel {
  min-height: 0;
  background: linear-gradient(180deg, #fffaf1 0%, #fff 100%);
}

.manuscript-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.manuscript-panel .panel-title-row {
  margin-bottom: 8px;
}

.manuscript-empty {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.manuscript-frame {
  flex: 1;
  min-height: 0;
  border: 1px solid #dbcbb2;
  border-radius: 12px;
  background: linear-gradient(180deg, #f4f2ec 0%, #ece7dc 100%);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  position: relative;
}

.manuscript-toolbar {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 2;
  display: inline-flex;
  gap: 6px;
}

.manuscript-toolbar button {
  min-width: 36px;
  height: 30px;
  border: 1px solid rgba(29, 42, 58, 0.26);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: #2a3542;
  font-size: 0.9rem;
  line-height: 1;
  padding: 0 10px;
  cursor: pointer;
}

.manuscript-image {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(26, 31, 40, 0.24);
  transition: transform 120ms ease-out;
  cursor: zoom-in;
  user-select: none;
}

.manuscript-image.is-dragging {
  transition: none;
  cursor: grabbing;
}

.segments {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding-left: 4px;
}

.fluent-text {
  direction: rtl;
  text-align: right;
  line-height: 1.75;
  font-size: 1.22rem;
  font-family: "Frank Ruhl Libre", "Times New Roman", serif;
  background: #fff;
  border: 1px solid #e7dbc6;
  border-radius: 12px;
  padding: 12px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.9);
}

.sentence {
  display: inline;
  padding: 2px 3px;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 140ms ease, box-shadow 140ms ease;
}

.steinsaltz-inline {
  display: inline;
  font-size: 0.8em;
  line-height: 1.6;
  color: #4e596e;
}

.sentence:hover {
  background: #f4e2c6;
  box-shadow: 0 0 0 1px rgba(197, 136, 74, 0.26);
}

.sage-mention {
  border-bottom: 1px dotted rgba(31, 95, 122, 0.75);
  cursor: help;
  position: relative;
}

.sage-mention:hover {
  color: #1a5871;
}

.sage-tooltip-floating {
  position: fixed;
  z-index: 9999;
  min-width: 220px;
  max-width: 320px;
  white-space: pre-line;
  background: #1f2430;
  color: #fff;
  border-radius: 10px;
  padding: 7px 9px;
  font-family: "Assistant", sans-serif;
  font-size: 0.82rem;
  line-height: 1.35;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.28);
  pointer-events: none;
  display: none;
}

.sentence.locked {
  background: #ebcda1;
  box-shadow: 0 0 0 1px rgba(146, 92, 41, 0.38);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}

.chip {
  border: 1px solid #d8c9b2;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.79rem;
  color: #413b2f;
  background: linear-gradient(180deg, #fff7ea 0%, #f7ecda 100%);
}

.chip-filter {
  cursor: pointer;
}

button.chip {
  background: linear-gradient(180deg, #fff7ea 0%, #f7ecda 100%);
  color: #413b2f;
  border: 1px solid #d8c9b2;
  box-shadow: none;
}

button.chip:hover:not(:disabled) {
  filter: brightness(0.98);
}

button.chip:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.chip-filter.active {
  background: linear-gradient(180deg, #e8f3f8 0%, #d6eaf3 100%);
  border-color: rgba(31, 95, 122, 0.35);
  color: #1f5f7a;
}

.scroll-region {
  height: 540px;
  overflow-y: auto;
  padding-left: 6px;
  display: grid;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

/* Fixed internal scrolling areas (desktop-style). */
.commentary-panel .scroll-region {
  flex: 1;
  min-height: 0;
  height: auto;
}

.halakha-panel .scroll-region,
.tanakh-panel .scroll-region {
  flex: 1;
  min-height: 0;
  height: auto;
}

.ref-card {
  background: linear-gradient(180deg, #fff 0%, #fffaf2 100%);
  border: 1px solid #e5d8c2;
  border-radius: 12px;
  padding: 9px;
  box-shadow: 0 4px 14px rgba(68, 52, 28, 0.08);
}

.genizah-compare {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 4px;
  align-items: stretch;
}

.genizah-witness {
  min-width: 260px;
  max-width: 300px;
  background: #f8f9fb;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.genizah-witness.is-base {
  background: linear-gradient(180deg, #fff7e3 0%, #fff3db 100%);
  border-color: #e7cc98;
}

.genizah-witness .ref-title::after {
  content: "";
}

.genizah-witness.is-base .ref-title::after {
  content: " (נוסח בסיס)";
  color: #8a5b13;
  font-weight: 700;
}

.genizah-diff-word {
  color: #c52f2f;
  font-weight: 700;
}

.genizah-witness .ref-text {
  flex: 1;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  padding-left: 4px;
}

.ref-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 6px;
  color: var(--brand-2);
  font-weight: 700;
  font-size: 0.92rem;
}

.genizah-image-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  border: 1px solid #d7c8ae;
  background: #fff;
  text-decoration: none;
  font-size: 0.82rem;
  line-height: 1;
}

.genizah-image-link:hover {
  border-color: #9fb6c4;
  background: #f4f9fc;
}

body.lightbox-open {
  overflow: hidden;
}

.genizah-lightbox[hidden] {
  display: none !important;
}

.genizah-lightbox {
  position: fixed;
  inset: 0;
  z-index: 2200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 23, 30, 0.78);
  padding: 18px;
}

.genizah-lightbox-toolbar {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 2;
  display: inline-flex;
  gap: 8px;
}

.genizah-lightbox-toolbar button {
  min-width: 40px;
  height: 34px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 999px;
  background: rgba(10, 12, 18, 0.75);
  color: #fff;
  font-size: 0.98rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 12px;
}

.genizah-lightbox-content {
  position: relative;
  max-width: min(1200px, 94vw);
  max-height: min(90vh, 980px);
  border-radius: 12px;
  overflow: auto;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: #0f1118;
}

.genizah-lightbox-image {
  display: block;
  max-width: min(1200px, 94vw);
  max-height: min(90vh, 980px);
  width: auto;
  height: auto;
  transition: transform 120ms ease-out;
  cursor: zoom-in;
  user-select: none;
}

.genizah-lightbox-image.is-dragging {
  transition: none;
  cursor: grabbing;
}

.genizah-lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 1;
  width: 34px;
  height: 34px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 999px;
  background: rgba(10, 12, 18, 0.75);
  color: #fff;
  font-size: 1.45rem;
  line-height: 1;
  cursor: pointer;
}

.ref-text,
.quote-box {
  margin: 0;
  direction: rtl;
  text-align: right;
  line-height: 1.32;
}

body.settings-rashi-script .commentary-panel .ref-text,
body.settings-rashi-script .commentary-panel .quote-box {
  font-family: "Noto Rashi Hebrew", "Rashi Script MT", "David Libre", serif;
  font-size: 1.03rem;
}

.commentary-panel .ref-text,
.commentary-panel .quote-box {
  font-family: "Frank Ruhl Libre", serif;
  font-size: 1.03rem;
}

.halakha-panel .ref-text,
.tanakh-panel .ref-text {
  font-family: "Frank Ruhl Libre", serif;
}

.tanakh-panel .ref-text {
  font-family: "Noto Serif Hebrew", "SBL Hebrew", "Ezra SIL", "Frank Ruhl Libre", serif;
  line-height: 1.6;
}

.rashi-piece {
  margin-top: 7px;
  padding-top: 7px;
  border-top: 1px dashed #dbc8ac;
}

.rashi-piece:first-of-type {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
}

.rashi-piece strong {
  color: var(--brand);
  font-weight: 700;
}

.subhead {
  margin: 10px 0 6px;
  font-size: 1rem;
  font-weight: 700;
  color: #3c414d;
}

.quote-box {
  min-height: 150px;
  max-height: 220px;
  overflow-y: auto;
  background: linear-gradient(180deg, #fff 0%, #fffaf2 100%);
  border: 1px solid #e5d8c2;
  border-radius: 12px;
  padding: 10px;
}
