*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0c0b24;
  --fg: #9affd0;
  --toolbar-h: 90px; /* 32px bottom offset + 58px bar height */
}

html { height: 100%; }
body {
  font-family: 'Inter', -apple-system, sans-serif;
  background: #D0D0D0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

/* ── Frame ── */
#frame {
  position: relative;
  background: var(--bg);
  overflow: visible;
  width: 100vw;
  min-height: 100dvh;
  padding-bottom: calc(var(--toolbar-h) + 16px);
}

/* ── Blocks ── */
.block {
  position: absolute;
  color: var(--fg);
  word-break: break-word;
  outline: none;
  cursor: text;
  white-space: pre-wrap;
  border: 3px solid transparent;
  border-radius: 0;
  min-height: 1.2em;
  -webkit-user-select: text;
  user-select: text;
  transition: border-color 0.12s;
}
.block.selected { border-color: var(--fg); }
.block[data-empty="true"]::before {
  content: attr(data-placeholder);
  opacity: 0.35;
  pointer-events: none;
}
.mc { display: inline; }

/* ── Grid layer ── */
#grid-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  transition: opacity 0.2s;
}
.ruler-strip {
  position: absolute;
  top: 0;
  bottom: 0;
}
.ruler-l { left: 0; border-right: 1px solid var(--fg); }
.ruler-r { right: 0; border-left: 1px solid var(--fg); }
.h-line {
  position: absolute;
  height: 1px;
  background: var(--fg);
}
.m-label {
  position: absolute;
  font-size: 11px;
  font-weight: 700;
  color: var(--fg);
  line-height: 1;
  font-family: 'Inter', sans-serif;
}

/* ── Drag layer ── */
#drag-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 8;
}
.dz-h {
  position: absolute;
  left: 0; right: 0;
  height: 24px;
  transform: translateY(-50%);
  cursor: ns-resize;
  pointer-events: all;
  touch-action: none;
}
.dz-v {
  position: absolute;
  top: 0;
  bottom: var(--toolbar-h);
  width: 24px;
  cursor: ew-resize;
  pointer-events: all;
  touch-action: none;
}

/* ── Toolbar ── */
#toolbar {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  display: flex;
  align-items: center;
  padding: 13px 14px;
  gap: 12px;
  z-index: 20;
  transition: opacity 0.2s;
}
.tb-icon, .tb-add {
  width: 32px; height: 32px;
  border-radius: 0;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  background: transparent;
  border: none;
  color: var(--bg);
  -webkit-tap-highlight-color: transparent;
  transition: background 0.1s, color 0.1s;
}
.tb-icon:active, .tb-add:active { background: var(--bg); color: var(--fg); }
.tb-icon .material-symbols-outlined,
.tb-add .material-symbols-outlined {
  font-size: 24px;
  font-variation-settings: 'wght' 400;
}

/* ── Block contextual buttons (edit + css) ── */
#edit-btn, #css-btn {
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  background: var(--fg);
  color: var(--bg);
  z-index: 15;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  opacity: 0;
  pointer-events: none;
}
#edit-btn.visible, #css-btn.visible { opacity: 1; pointer-events: auto; }
#edit-btn .material-symbols-outlined,
#css-btn .material-symbols-outlined { font-size: 24px; font-variation-settings: 'wght' 400; }

/* ── Color swatches ── */
.color-swatch {
  width: 32px; height: 32px;
  border-radius: 0;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
  display: flex;
  -webkit-tap-highlight-color: transparent;
}
.color-swatch input[type="color"] {
  position: absolute;
  inset: 0;
  width: 140%;
  height: 140%;
  top: -20%;
  left: -20%;
  opacity: 0;
  cursor: pointer;
  border: none;
  padding: 0;
}
#bg-swatch { background: var(--bg); }
#fg-swatch { background: var(--fg); border: 1px solid var(--bg); }

/* ── Panel backdrop (SP only) ── */
#panel-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 29;
  pointer-events: none;
}
#panel-backdrop.active {
  pointer-events: auto;
}

/* ── Font panel ── */
#font-panel {
  position: fixed;
  right: 32px;
  bottom: 32px;
  transform: translateX(calc(100% + 32px));
  width: 380px;
  max-height: 80dvh;
  background: var(--fg);
  z-index: 30;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  contain: layout paint;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
#fp-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
#font-panel.open { transform: translateX(0); }

/* ── SP half-modal (≤768px) ── */
@media (max-width: 768px) {
  #panel-backdrop { display: block; }

  #font-panel {
    width: 100%;
    right: 0;
    bottom: 0;
    transform: translateY(110vh);
    max-height: 50dvh;
  }
  #font-panel.open { transform: translateY(0); }
  body.ui-off #font-panel { transform: translateY(110vh) !important; }
}

.fp-handle { display: none; }

.fp-header {
  display: flex; align-items: center; justify-content: flex-end;
  padding: 16px 20px;
  border-bottom: 1px solid var(--bg);
}
.fp-close-btn {
  width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: var(--bg);
  background: transparent;
  border: none;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}
.fp-close-btn .material-symbols-outlined { font-size: 24px; }

/* ── Slider rows ── */
.fp-sl-row {
  height: 45px;
  display: flex; align-items: center;
  gap: 20px;
}
.fp-sl-left {
  width: 100px; flex-shrink: 0;
  display: flex; flex-direction: column; gap: 8px;
}
.fp-sl-right {
  flex: 1;
  display: flex; align-items: center;
}
.fp-sl-name {
  font-size: 11px; font-weight: 700; color: var(--bg);
  white-space: nowrap; line-height: 1;
}
.fp-sl-val {
  font-size: 20px; font-weight: 500; color: var(--bg);
  font-variant-numeric: tabular-nums; line-height: 1;
}
.fp-slider {
  -webkit-appearance: none;
  width: 100%; height: 2px;
  background: var(--bg);
  outline: none; cursor: pointer;
}
.fp-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--bg);
  cursor: pointer;
}

/* ── Tabs ── */
.fp-tabs {
  margin-top: 40px;
  display: flex;
}
.fp-tab {
  flex: 1;
  padding: 12px 4px;
  font-size: 13px; font-weight: 700;
  text-align: center;
  cursor: pointer;
  background: transparent;
  border-top: 1px solid var(--bg);
  border-bottom: 1px solid var(--bg);
  border-right: 1px solid var(--bg);
  border-left: none;
  color: var(--bg);
  font-family: 'Inter', sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.fp-tab:first-child { border-left: 1px solid var(--bg); }
.fp-tab.active { background: var(--bg); color: var(--fg); }

/* ── Panel item groups ── */
.fp-sec-label {
  font-size: 11px; font-weight: 700;
  color: var(--bg); line-height: 1;
}

/* Font selector with label above */
.fp-font-group {
  display: flex; flex-direction: column; gap: 4px;
}

/* Text Align + Italic side by side */
.fp-align-italic-row {
  display: flex; gap: 16px; align-items: flex-start;
}
.fp-align-group {
  display: flex; flex-direction: column; gap: 8px;
}
.fp-italic-group {
  display: flex; flex-direction: column; gap: 8px;
  width: 64px; flex-shrink: 0;
}

/* Font Feature group */
.fp-ffs-group {
  display: flex; flex-direction: column; gap: 8px;
}

/* Generic button row */
.fp-btn-row {
  display: flex; gap: 16px;
}

/* ── Font selector ── */
.fp-font-section {
  margin-top: 20px;
}
.fp-font-sel {
  display: flex; align-items: center;
  width: 100%;
  background: transparent;
  border: 1px solid var(--bg);
  color: var(--bg);
  padding: 4px 4px 4px 8px;
  cursor: pointer;
  font-size: 20px; font-weight: 500;
  font-family: 'Inter', sans-serif;
  text-align: left;
  -webkit-tap-highlight-color: transparent;
}
.fp-font-sel-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fp-font-sel svg  { width: 24px; height: 24px; flex-shrink: 0; }

/* ── Font Feature Settings (legacy, keep for font view) ── */
.fp-ffs-section { display: flex; flex-direction: column; gap: 7px; }
.fp-ffs-row { display: flex; gap: 8px; flex-wrap: wrap; }
.ffs-btn {
  width: 64px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--bg);
  background: transparent;
  color: var(--bg);
  font-size: 13px; font-weight: 700;
  cursor: pointer;
  font-family: 'Inter', sans-serif;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}
.ffs-btn .material-symbols-outlined { font-size: 20px; font-variation-settings: 'wght' 400; }
.ffs-btn.on { background: var(--bg); color: var(--fg); }

/* ── Font row ── */
.fp-font-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
}
.fp-font-row .fp-font-sel { flex: 1; min-width: 0; }

/* ── CSS panel ── */
#css-panel {
  position: fixed;
  right: 32px;
  bottom: 32px;
  transform: translateX(calc(100% + 32px));
  width: 380px;
  max-height: 80dvh;
  background: var(--fg);
  z-index: 30;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
#css-panel.open { transform: translateX(0); }
#cp-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px 20px 32px;
}
.cp-text {
  font-family: monospace;
  font-size: 13px;
  line-height: 1.7;
  color: var(--bg);
  white-space: pre-wrap;
  word-break: break-all;
  margin: 0;
}
@media (max-width: 768px) {
  #css-panel {
    width: 100%;
    right: 0;
    bottom: 0;
    transform: translateY(110vh);
    max-height: 50dvh;
  }
  #css-panel.open { transform: translateY(0); }
  body.ui-off #css-panel { transform: translateY(110vh) !important; }
}

/* ── CSS panel selector row ── */
.cp-sel-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}
.cp-sel-label {
  font-size: 11px; font-weight: 700;
  color: var(--bg); white-space: nowrap; flex-shrink: 0;
}
.cp-sel-input {
  flex: 1; min-width: 0;
  border: 1px solid var(--bg);
  background: transparent;
  color: var(--bg);
  font-size: 13px; font-weight: 500;
  font-family: monospace;
  padding: 5px 8px;
  outline: none;
}

/* ── Copy CSS button ── */
.fp-copy-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  margin-top: 32px;
  padding: 12px 16px;
  border: 1px solid var(--bg);
  background: transparent;
  color: var(--bg);
  font-size: 13px;
  font-weight: 700;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.1s, color 0.1s;
}
.fp-copy-btn .material-symbols-sharp {
  font-size: 20px;
  font-variation-settings: 'wght' 400;
}
.fp-copy-btn:active,
.fp-copy-btn.copied { background: var(--bg); color: var(--fg); }

/* ── Font view (in-panel slide) ── */
#fp-font-view {
  position: absolute;
  inset: 0;
  background: var(--fg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: transform;
}
#fp-font-view.open { transform: translateX(0); }

.fpv-back {
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  background: transparent;
  border: 1px solid var(--bg);
  color: var(--bg);
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}
.fpv-back .material-symbols-outlined { font-size: 24px; font-variation-settings: 'wght' 400; }

#fpv-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
}
.fpv-status {
  padding: 6px 20px;
  font-size: 11px; font-weight: 700;
  color: var(--bg);
  opacity: 0.5;
  border-bottom: 1px solid var(--bg);
}
.fpv-key-section {
  padding: 12px 20px;
  border-bottom: 1px solid var(--bg);
}
.fpv-key-label {
  font-size: 11px; font-weight: 600;
  color: var(--bg);
  margin-bottom: 4px; line-height: 1.4;
}
.fpv-key-hint a { font-size: 10px; color: var(--bg); opacity: 0.6; text-decoration: underline; }
.fpv-key-row { display: flex; gap: 6px; align-items: center; margin-top: 6px; }
.fpv-key-input {
  flex: 1; min-width: 0;
  border: 1px solid var(--bg);
  background: transparent; color: var(--bg);
  font-size: 12px; padding: 5px 8px;
  outline: none; font-family: monospace;
}
.fpv-key-btn {
  padding: 5px 12px;
  border: 1px solid var(--bg);
  background: transparent; color: var(--bg);
  font-size: 11px; font-weight: 700;
  cursor: pointer; white-space: nowrap; flex-shrink: 0;
}
.fpv-key-btn:active { background: var(--bg); color: var(--fg); }
.fpv-search-wrap {
  padding: 20px 20px 8px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  background: var(--fg);
  z-index: 1;
}
.fpv-search-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.fpv-search-row .fpv-search { flex: 1; }
.fpv-lang-filter {
  margin-top: 8px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.fpv-lang-toggle {
  width: 96px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--bg);
  background: transparent;
  color: var(--bg);
  font-size: 11px; font-weight: 700;
  font-family: 'Inter', sans-serif;
  flex-shrink: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.fpv-lang-toggle.active { background: var(--bg); color: var(--fg); }
.fpv-lang-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  overflow: hidden;
  max-width: 0;
  max-height: 0;
  transition: max-width 0.25s ease, max-height 0.25s ease;
}
.fpv-lang-chips.open     { max-width: 600px; max-height: 130px; }
.fpv-lang-chips.resident { max-width: 80px;  max-height: 40px;  }
.fpv-lang-chips.resident .fpv-lang-chip        { display: none; }
.fpv-lang-chips.resident .fpv-lang-chip.active { display: flex; }
.fpv-lang-chip {
  width: 64px; height: 28px;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--bg);
  background: transparent;
  color: var(--bg);
  font-size: 11px; font-weight: 700;
  cursor: pointer;
  font-family: 'Inter', sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.fpv-lang-chip.active { background: var(--bg); color: var(--fg); }
.fpv-search {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  border: 1px solid var(--bg);
  background: var(--fg); color: var(--bg);
  font-size: 20px; font-weight: 500;
  padding: 4px 4px 4px 8px;
  outline: none; box-sizing: border-box;
}
.fpv-search::placeholder { color: var(--bg); opacity: 0.4; }
.fpv-list { flex: 1; }
.fpv-item {
  padding: 8px 20px;
  font-size: 20px; font-weight: 500;
  color: var(--bg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.fpv-item:active, .fpv-item.cur { background: var(--bg); color: var(--fg); }
.fpv-empty {
  padding: 16px 20px;
  font-size: 13px; color: var(--bg); opacity: 0.4;
}


/* ── Hidden UI state ── */
body.ui-off #grid-layer { opacity: 0; }
body.ui-off #toolbar { opacity: 0; pointer-events: none; }
body.ui-off #edit-btn, body.ui-off #css-btn { opacity: 0; pointer-events: none; }
body.ui-off #font-panel {
  transition: none !important;
  transform: translateX(calc(100% + 32px)) !important;
}
body.ui-off #css-panel {
  transition: none !important;
  transform: translateX(calc(100% + 32px)) !important;
}
@media (max-width: 768px) {
  body.ui-off #font-panel { transform: translateY(110vh) !important; }
}
body.ui-off .block { border-color: transparent !important; }
