* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* ── Theme Variables ───────────────────────────────── */
.theme-green {
  --bg-primary: #1a1a1a;
  --bg-secondary: #0f0f0f;
  --bg-tertiary: #0a0a0a;
  --fg-primary: #00f000;
  --fg-secondary: #00ff00;
  --fg-dim: #008000;
  --border-color: #004000;
  --accent: #00c000;
  --selection: rgba(0, 240, 0, 0.2);
}

.theme-amber {
  --bg-primary: #1a1200;
  --bg-secondary: #0f0900;
  --bg-tertiary: #0a0600;
  --fg-primary: #ffb000;
  --fg-secondary: #ffc000;
  --fg-dim: #806000;
  --border-color: #403000;
  --accent: #ffc000;
  --selection: rgba(255, 176, 0, 0.2);
}

.theme-blue {
  --bg-primary: #001a2a;
  --bg-secondary: #000f1a;
  --bg-tertiary: #000a12;
  --fg-primary: #00a0ff;
  --fg-secondary: #00b0ff;
  --fg-dim: #005080;
  --border-color: #002840;
  --accent: #00b0ff;
  --selection: rgba(0, 160, 255, 0.2);
}

.theme-paper {
  --bg-primary: #f5f5dc;
  --bg-secondary: #e8e8d0;
  --bg-tertiary: #d8d8c0;
  --fg-primary: #2a2a2a;
  --fg-secondary: #1a1a1a;
  --fg-dim: #8a8a7a;
  --border-color: #c0c0a0;
  --accent: #4a6a4a;
  --selection: rgba(42, 42, 42, 0.15);
}

.theme-dark {
  --bg-primary: #1e1e1e;
  --bg-secondary: #141414;
  --bg-tertiary: #0a0a0a;
  --fg-primary: #d4d4d4;
  --fg-secondary: #e0e0e0;
  --fg-dim: #666666;
  --border-color: #333333;
  --accent: #569cd6;
  --selection: rgba(212, 212, 212, 0.15);
}

/* ── Layout ────────────────────────────────────────── */
html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--fg-primary);
  display: flex;
  flex-direction: column;
}

/* ── Menu Bar (always visible) ─────────────────────── */
.menu-zone {
  position: relative;
  flex-shrink: 0;
  z-index: 100;
}

/* ── Tab Bar ───────────────────────────────────────── */
.tab-bar-outer {
  display: flex;
  align-items: stretch;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.tab-bar {
  display: flex;
  align-items: stretch;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) var(--bg-tertiary);
  position: relative;
}

.tab-bar::-webkit-scrollbar { height: 4px; }
.tab-bar::-webkit-scrollbar-track { background: var(--bg-tertiary); }
.tab-bar::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 2px; }

.tab {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  font-family: inherit;
  font-size: 0.75rem;
  color: var(--fg-dim);
  background: transparent;
  border: none;
  border-right: 1px solid var(--border-color);
  cursor: pointer;
  white-space: nowrap;
  min-width: 120px;
  max-width: 180px;
  transition: color 0.1s, background 0.1s;
}

.tab:hover,
.tab:focus-visible { color: var(--fg-primary); background: var(--bg-secondary); outline: none; }

.tab.active {
  color: var(--fg-primary);
  background: var(--bg-primary);
  border-bottom: 2px solid var(--accent);
}

.tab-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab-close {
  flex-shrink: 0;
  background: none;
  border: none;
  color: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  opacity: 0;
  padding: 0 2px;
  line-height: 1;
  transition: opacity 0.1s;
}

.tab:hover .tab-close,
.tab.active .tab-close { opacity: 0.6; }
.tab-close:hover { opacity: 1 !important; }

.tab.tab-dragging { opacity: 0.4; }
.tab.tab-drop-before { box-shadow: inset 3px 0 0 var(--accent); }
.tab.tab-drop-after  { box-shadow: inset -3px 0 0 var(--accent); }

.tab-new {
  padding: 0.4rem 0.75rem;
  background: none;
  border: none;
  border-left: 1px solid var(--border-color);
  color: var(--fg-dim);
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.1s;
}

.tab-new:hover { color: var(--fg-primary); }

/* ── Editor Area ───────────────────────────────────── */
.editor-area {
  flex: 1;
  min-height: 0;
  position: relative;
}

#editor-container {
  width: 100%;
  height: 100%;
}

.iwritem-editor-wrapper {
  width: 100%;
  height: 100%;
  position: relative;
}

.iwritem-editor {
  width: 100%;
  height: 100%;
  background: transparent;
  color: var(--fg-primary);
  caret-color: var(--fg-primary);
  border: none;
  outline: none;
  resize: none;
  padding: 1.5rem 2rem;
  font-family: 'Courier New', Consolas, 'Liberation Mono', monospace;
  font-size: 16px;
  line-height: 1.6;
  overflow: auto;
  position: relative;
  z-index: 2;
  tab-size: 4;
}

.iwritem-editor::selection {
  color: var(--fg-primary);
  -webkit-text-fill-color: var(--fg-primary);
  background: var(--selection);
}

.iwritem-editor.iwritem-underlay-hidden::selection {
  color: transparent;
  -webkit-text-fill-color: transparent;
}

.text-mirror {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 1.5rem 2rem;
  font-family: 'Courier New', Consolas, 'Liberation Mono', monospace;
  font-size: 16px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  pointer-events: none;
  z-index: 1;
  color: var(--fg-primary);
  border: none;
  overflow: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  tab-size: 4;
}

.text-mirror::-webkit-scrollbar { display: none; }

.drag-highlight {
  outline: 3px dashed var(--accent);
  outline-offset: -3px;
}

/* ── Status Bar ────────────────────────────────────── */
.status-bar {
  background: var(--bg-tertiary);
  border-top: 1px solid var(--border-color);
  padding: 0.25rem 1rem;
  font-size: 0.7rem;
  font-family: monospace;
  color: var(--fg-dim);
  display: flex;
  gap: 1.5rem;
  flex-shrink: 0;
}

/* ── Modal Overrides ───────────────────────────────── */
.iwritem-modal-dialog {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  color: var(--fg-primary);
  padding: 0;
  max-width: 600px;
  min-width: 300px;
  margin: auto;
  border-radius: 4px;
  overflow: hidden;
}

.iwritem-modal-dialog::backdrop {
  background: rgba(0, 0, 0, 0.7);
}

.modal-content { display: flex; flex-direction: column; width: 100%; max-height: 80vh; }
.modal-header { padding: 0.75rem 1rem; border-bottom: 1px solid var(--border-color); display: flex; justify-content: space-between; align-items: center; flex-shrink: 0; }
.modal-title { font-size: 0.9rem; font-weight: 600; }
.modal-close-btn { background: none; border: none; color: var(--fg-primary); font-size: 1.25rem; cursor: pointer; padding: 0; width: 1.5rem; height: 1.5rem; line-height: 1; }
.modal-body { padding: 1.25rem; line-height: 1.6; overflow-y: auto; flex: 1; min-height: 0; font-size: 0.85rem; }
.modal-footer { padding: 0.75rem 1rem; border-top: 1px solid var(--border-color); display: flex; justify-content: flex-end; gap: 0.5rem; flex-shrink: 0; }
.modal-btn { padding: 0.4rem 1rem; border: 1px solid var(--border-color); cursor: pointer; font-family: inherit; font-size: 0.8rem; border-radius: 3px; }
.modal-btn-primary { background: var(--fg-primary); color: var(--bg-tertiary); }
.modal-btn-secondary { background: var(--bg-tertiary); color: var(--fg-primary); }
.modal-input { width: 100%; background: var(--bg-tertiary); border: 1px solid var(--border-color); color: var(--fg-primary); padding: 0.5rem; font-family: inherit; font-size: 0.85rem; margin-top: 0.75rem; border-radius: 3px; outline: none; }
.modal-input:focus { border-color: var(--accent); }

/* ── Recording Indicator ───────────────────────────── */
.recording-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  background: #ff3333;
  border-radius: 50%;
  animation: rec-pulse 1s infinite;
  margin-right: 4px;
  vertical-align: middle;
}

@keyframes rec-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.2; }
}

/* ── Scrollbar ─────────────────────────────────────── */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-tertiary); }
::-webkit-scrollbar-thumb { background: var(--border-color); }
::-webkit-scrollbar-thumb:hover { background: var(--fg-dim); }

/* ── Touch devices: larger targets ─────────────────── */
@media (pointer: coarse) {
  /* Tabs: 44px minimum touch targets */
  .tab     { min-height: 44px; padding: 0 0.875rem; }
  .tab-new { min-height: 44px; padding: 0 0.875rem; }
  /* Close button: always visible, larger hit area */
  .tab-close {
    opacity: 0.5 !important;
    padding: 0 0.5rem;
    min-height: 44px;
    display: flex;
    align-items: center;
  }
  /* Hide scrollbar on touch — swipe navigates */
  .tab-bar { scrollbar-width: none; -ms-overflow-style: none; }
  .tab-bar::-webkit-scrollbar { display: none; }
  /* Fade hint: signals more tabs to the right */
  .tab-bar::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 48px;
    height: 100%;
    background: linear-gradient(to right, transparent, var(--bg-tertiary));
    pointer-events: none;
  }
}
