/* ==========================================================================
   TermaType — Editor & Writing Surface
   The heart of the application: where Tibetan meets the page.
   ========================================================================== */

/* ── EDITOR SCROLL CONTAINER ──────────────────────────────────────────────── */
#editor-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--c-bg);
  padding: 48px 32px 96px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ── THE PAGE ─────────────────────────────────────────────────────────────── */
#editor-page {
  width: 100%;
  max-width: var(--w-page-max);
  /* Real paper height when set (A4, Letter, A5); otherwise fill viewport */
  min-height: var(--h-page-min, calc(100vh - var(--h-titlebar) - var(--h-toolbar) - var(--h-statusbar) - 120px));
  background: var(--c-page);
  border-radius: var(--r-page);
  box-shadow: var(--shadow-page);
  overflow: hidden;
  flex-shrink: 0;
}

/* ── THE EDITOR CONTENTEDITABLE ───────────────────────────────────────────── */
#editor {
  min-height: 100%;
  padding: var(--page-pad);
  outline: none;
  user-select: text;
  caret-color: var(--c-accent);

  /* Default Tibetan typography */
  font-family: 'Jomolhari', 'Noto Serif Tibetan', serif;
  font-size: 18px;
  line-height: 2.0;
  color: var(--c-text);
  word-break: keep-all;
  overflow-wrap: anywhere;
  -webkit-font-smoothing: antialiased;

  /* Prevent default contenteditable styling */
  -webkit-user-modify: read-write;
}

/* Placeholder — shown when editor is empty.
   ::before must live on #editor itself so attr(data-placeholder) resolves correctly. */
#editor.is-empty {
  position: relative;
}

#editor.is-empty::before {
  content: attr(data-placeholder);
  color: var(--c-text-3);
  font-family: 'Jomolhari', 'Noto Serif Tibetan', serif;
  font-size: 18px;
  line-height: 2.0;
  pointer-events: none;
  position: absolute;
  top: var(--page-pad);
  left: var(--page-pad);
  right: var(--page-pad);
}

/* ── EDITOR TYPOGRAPHY ─────────────────────────────────────────────────────── */
#editor p {
  margin-bottom: 0.5em;
  line-height: inherit;
}

#editor p:last-child { margin-bottom: 0; }

#editor h1, #editor h2, #editor h3,
#editor h4, #editor h5, #editor h6 {
  font-family: 'Jomolhari', 'Noto Serif Tibetan', serif;
  color: var(--c-text);
  line-height: 1.5;
  margin: 1.2em 0 0.6em;
}

#editor h1 { font-size: 2.2em; }
#editor h2 { font-size: 1.7em; }
#editor h3 { font-size: 1.35em; }
#editor h4 { font-size: 1.15em; }

#editor h1:first-child,
#editor h2:first-child { margin-top: 0; }

#editor ul, #editor ol {
  padding-left: 1.8em;
  margin-bottom: 0.85em;
}

#editor li {
  margin-bottom: 0.3em;
  line-height: inherit;
}

#editor blockquote {
  border-left: 3px solid var(--c-crimson);
  padding-left: 1.2em;
  margin: 1em 0;
  color: var(--c-text-2);
  font-style: italic;
}

#editor hr {
  border: none;
  border-top: 1px solid var(--c-border);
  margin: 1.5em 0;
}

#editor a {
  color: var(--c-accent-dark);
  text-decoration: underline;
}

/* ── TABLES ────────────────────────────────────────────────────────────────── */
#editor table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.2em 0;
  font-size: 0.95em;
  table-layout: fixed;
}

#editor th, #editor td {
  border: 1px solid var(--c-border-2);
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
  min-width: 80px;
  line-height: 1.8;
}

#editor th {
  background: var(--c-panel-header);
  font-weight: 600;
  font-size: 0.9em;
  color: var(--c-text-2);
}

#editor tr:hover td {
  background: rgba(242, 237, 230, 0.4);
}

/* Table selected state */
#editor table.selected-table {
  outline: 2px solid var(--c-accent);
}

@media (max-width: 600px) {
  #editor th, #editor td {
    min-width: 40px;
    padding: 4px 6px;
  }
}

/* ── IMAGES IN EDITOR ──────────────────────────────────────────────────────── */
#editor img {
  max-width: 100%;
  height: auto;
  border-radius: var(--r-sm);
  display: block;
  margin: 1em auto;
  cursor: pointer;
  transition: box-shadow var(--t-fast);
}

#editor img:hover {
  box-shadow: 0 0 0 2px var(--c-accent);
}

#editor img.selected {
  outline: 2px solid var(--c-accent);
  outline-offset: 2px;
}

/* ── MIXED LANGUAGE AUTO-DETECTION ────────────────────────────────────────── */
/* Applied automatically by editor.js when both scripts are detected in a block */

/* Block contains both Tibetan and Latin — adjust line-height for readability */
#editor .has-mixed-content {
  line-height: 1.8;
}

/* Block is purely Latin/English — switch to UI font stack + Latin metrics */
#editor .is-latin-block {
  font-family: var(--font-ui);
  font-size: 1em;
  line-height: 1.6;
  word-break: normal;
  overflow-wrap: break-word;
}

/* ── VERTICAL ALIGNMENT FOR MIXED FONT SIZES ────────────────────────────────
   Tibetan script hangs from a HEAD LINE at the top — like a clothesline.
   Latin script sits on a BASELINE at the bottom.

   When font-size spans coexist on a line, the browser's default baseline
   alignment scatters Tibetan head marks vertically.  Fix:
     • text-top    → aligns each span's top with the parent's font top
     • line-height: 1 → eliminates half-leading so glyph tops actually meet
   Latin spans get baseline alignment via the .tt-latin class (set by JS). */

#editor span[style*="font-size"] {
  vertical-align: text-top;
  line-height: 1;
}

#editor span[style*="font-size"].tt-latin {
  vertical-align: baseline;
  line-height: inherit;
}

/* ── SELECTION STYLE ───────────────────────────────────────────────────────── */
#editor ::selection {
  background: rgba(201, 122, 30, 0.18);
  color: inherit;
}

/* ── STATUS BAR ────────────────────────────────────────────────────────────── */
#status-bar {
  height: var(--h-statusbar);
  background: var(--c-statusbar);
  border-top: 1px solid var(--c-border);
  display: flex;
  align-items: center;
  padding: 0 14px;
  gap: 0;
  flex-shrink: 0;
}

.sb-item {
  font-size: 11.5px;
  color: var(--c-text-3);
  padding: 0 10px;
  border-right: 1px solid var(--c-border);
  line-height: 1;
  white-space: nowrap;
}

.sb-item:last-child { border-right: none; }
.sb-item:first-child { padding-left: 0; }

.sb-item strong {
  color: var(--c-text-2);
  font-weight: 600;
}

.sb-item.sb-ime-active strong {
  color: var(--c-accent);
}

.sb-spacer { flex: 1; }

.sb-beta {
  background: var(--c-accent);
  color: white !important;
  font-size: 9px !important;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 1px 6px !important;
  border-radius: 3px;
  border-right: none !important;
}

.sb-learn-more {
  font-size: 10.5px !important;
  color: var(--c-accent) !important;
  text-decoration: none;
  cursor: pointer;
}
.sb-learn-more:hover { text-decoration: underline; }

/* ── FLOATING TOOLBAR (text selection) ─────────────────────────────────────── */
#float-toolbar {
  position: fixed;
  background: var(--c-titlebar);
  border-radius: var(--r-md);
  padding: 4px;
  display: flex;
  align-items: center;
  gap: 1px;
  box-shadow: var(--shadow-popup);
  z-index: 300;
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
  transition: opacity var(--t-base), transform var(--t-base);
}

#float-toolbar.visible {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0);
}

.ftb-btn {
  width: 28px;
  height: 28px;
  border: none;
  background: none;
  border-radius: var(--r-sm);
  cursor: pointer;
  color: rgba(255,255,255,0.75);
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--t-fast), color var(--t-fast);
}

.ftb-btn:hover {
  background: rgba(255,255,255,0.12);
  color: white;
}

.ftb-btn.active {
  color: var(--c-accent-light);
}

.ftb-sep {
  width: 1px;
  height: 16px;
  background: rgba(255,255,255,0.15);
  margin: 0 3px;
}

/* ── KEYBOARD GUIDE PANEL ─────────────────────────────────────────────────── */
#key-guide-panel {
  position: fixed;
  bottom: calc(var(--h-statusbar) + 12px);
  right: 20px;
  width: 320px;
  background: var(--c-titlebar);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-popup);
  overflow: hidden;
  z-index: 400;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity var(--t-slow), transform var(--t-slow);
}

#key-guide-panel.visible {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0);
}

.kg-header {
  padding: 12px 16px 10px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.kg-title {
  font-size: 12px;
  font-weight: 600;
  color: rgba(255,255,255,0.8);
  letter-spacing: 0.3px;
}

.kg-close {
  background: none;
  border: none;
  cursor: pointer;
  color: rgba(255,255,255,0.4);
  font-size: 14px;
  line-height: 1;
  padding: 0;
}

.kg-close:hover { color: white; }

.kg-body {
  padding: 12px;
  max-height: 320px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}

.kg-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 4px;
  border-radius: var(--r-sm);
}

.kg-key {
  font-family: var(--font-mono);
  font-size: 10px;
  background: rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.7);
  padding: 1px 5px;
  border-radius: 3px;
  min-width: 18px;
  text-align: center;
  border: 1px solid rgba(255,255,255,0.1);
  letter-spacing: 0;
}

.kg-char {
  font-family: 'Jomolhari', 'Noto Serif Tibetan', serif;
  font-size: 15px;
  color: var(--c-accent-light);
  min-width: 20px;
}

.kg-name {
  font-size: 10px;
  color: rgba(255,255,255,0.45);
  flex: 1;
}

/* ── MOBILE NOTICE ─────────────────────────────────────────────────────── */
#mobile-notice {
  display: none;
}

@media (max-width: 768px) {
  #mobile-notice {
    display: flex;
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: var(--c-bg, #F2EDE6);
    align-items: center;
    justify-content: center;
    padding: 24px;
  }

  #mobile-notice.dismissed {
    display: none;
  }

  .mobile-notice-inner {
    text-align: center;
    max-width: 340px;
  }

  .mobile-notice-icon {
    font-size: 48px;
    margin-bottom: 12px;
  }

  .mobile-notice-title {
    font-family: var(--font-ui, 'Inter', sans-serif);
    font-size: 24px;
    font-weight: 700;
    color: var(--c-text, #1C1916);
    margin-bottom: 4px;
  }

  .mobile-notice-sub {
    font-family: var(--font-ui, 'Inter', sans-serif);
    font-size: 12px;
    font-weight: 600;
    color: var(--c-accent, #C97A1E);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 20px;
  }

  .mobile-notice-text {
    font-family: var(--font-ui, 'Inter', sans-serif);
    font-size: 14px;
    line-height: 1.6;
    color: var(--c-text-2, #5C5650);
    margin: 0 0 20px;
  }

  .mobile-notice-link {
    display: block;
    font-family: var(--font-ui, 'Inter', sans-serif);
    font-size: 13px;
    color: var(--c-accent, #C97A1E);
    text-decoration: none;
    margin-bottom: 24px;
  }
  .mobile-notice-link:hover { text-decoration: underline; }

  .mobile-notice-btn {
    font-family: var(--font-ui, 'Inter', sans-serif);
    font-size: 13px;
    color: var(--c-text-3, #9A9490);
    background: none;
    border: 1px solid var(--c-border, #D8D3CC);
    border-radius: 6px;
    padding: 8px 20px;
    cursor: pointer;
    transition: all 0.15s ease;
  }
  .mobile-notice-btn:hover {
    background: var(--c-page, white);
    color: var(--c-text, #1C1916);
  }

  /* On mobile, reset page height to viewport-fill (no fixed paper height) */
  #editor-page {
    min-height: calc(100vh - var(--h-titlebar) - var(--h-toolbar) - var(--h-statusbar) - 60px) !important;
  }
}
