/* ===== CSS RESET & BASE ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg-color: #f9f5e8;
  --text-color: #2c4a6b;
  --panel-bg: #f0ead7;
  --border-color: #dfd7c0;
  --accent-color: #3d5a7a;
  --placeholder-color: #8a9ab0;
  --msg-user-color: #3d5a7a;
  --msg-user-bg: rgba(61, 90, 122, 0.08);
  --msg-system-color: #a08040;
  --msg-system-bg: rgba(160, 128, 64, 0.08);
  --hover-color: rgba(44, 74, 107, 0.06);
  --code-bg: #efe9d6;
  --touch-target-size: 48px;
  --mobile-padding: 16px;
  --panel-transition: 0.15s ease;
  --panel-z-mobile: 1000;
}

*:focus-visible { outline: 2px solid var(--accent-color); outline-offset: 2px; }

body {
  font-family: 'Merriweather', serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  overflow: hidden;
  height: 100dvh;
  transition: background-color 0.3s, color 0.3s;
}

/* ===== TOP BAR ===== */
#top-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 40px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  background: var(--bg-color);
  border-bottom: 1px solid var(--border-color);
  z-index: 100;
  user-select: none;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.top-bar-center {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  overflow: hidden;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.clickable {
  cursor: pointer;
  opacity: 0.8;
  transition: opacity 0.2s;
  user-select: none;
}
.clickable:hover { opacity: 1; }

#fieldwriter-toggle {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.3px;
  line-height: 1;
}

#file-name {
  font-size: 13px;
  opacity: 0.7;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#word-count {
  font-size: 11px;
  opacity: 0.4;
}

#assistant-toggle {
  font-size: 16px;
  padding: 4px 8px;
  border-radius: 4px;
  border: none;
  background: transparent;
  color: var(--text-color);
  cursor: pointer;
  opacity: 0.5;
  line-height: 1;
}
#assistant-toggle:hover { opacity: 1; }

/* ===== FORMATTING TOOLBAR (floating on text selection) ===== */
#formatting-toolbar {
  position: fixed;
  z-index: 500;
  display: flex;
  gap: 2px;
  padding: 4px 8px;
  background: var(--bg-color);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}
#formatting-toolbar.hidden { display: none; }

.fmt-btn {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 12px;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 3px;
  font-weight: 500;
  transition: background 0.15s;
}
.fmt-btn:hover { background: var(--hover-color); }
.fmt-btn.active { background: var(--accent-color); color: var(--bg-color); }

/* ===== MOBILE FORMATTING TOOLBAR (above keyboard) ===== */
#mobile-format-toolbar {
  position: fixed;
  left: 0;
  right: 0;
  height: 40px;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 8px;
  background: var(--panel-bg);
  border-top: 1px solid var(--border-color);
  z-index: 600;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
#mobile-format-toolbar.hidden { display: none; }

.mfmt-btn {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 14px;
  min-width: 36px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 4px;
  flex-shrink: 0;
  font-weight: 500;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.mfmt-btn:active { background: var(--hover-color); }

.mfmt-sep {
  width: 1px;
  height: 20px;
  background: var(--border-color);
  flex-shrink: 0;
  margin: 0 2px;
}

/* ===== LEFT PANEL (FILES) ===== */
#left-panel {
  position: fixed;
  top: 40px; left: 0; bottom: 0;
  width: 260px;
  background: var(--bg-color);
  border-right: 1px solid var(--border-color);
  z-index: 50;
  padding: 0;
  display: flex;
  flex-direction: column;
  transition: transform var(--panel-transition);
}
#left-panel.hidden {
  transform: translateX(-100%);
}

.panel-scroll-area {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  min-height: 0;
}

.panel-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  padding: 0 4px;
}

.panel-section-title {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  opacity: 0.45;
  color: var(--text-color);
}

.panel-actions {
  display: flex;
  gap: 4px;
}

.panel-actions button {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 13px;
  cursor: pointer;
  opacity: 0.55;
  padding: 4px 0;
  border-radius: 0;
  text-align: left;
}
.panel-actions button:hover { opacity: 0.85; background: none; }

/* Spelled-out action links */
.panel-actions-row {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0;
  margin-bottom: 12px;
}

.panel-action-link {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  opacity: 0.55;
  padding: 4px 0;
  transition: opacity 0.15s;
}
.panel-action-link:hover { opacity: 0.85; }

/* File items */
.file-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  margin: 2px 0;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
  font-size: 13px;
}
.file-item:hover { background: var(--hover-color); }
.file-item.active {
  background: var(--hover-color);
}
.file-item.dragging { opacity: 0.5; }

.file-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-actions, .folder-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.15s;
}
.file-item:hover .file-actions,
.folder-header:hover .folder-actions { opacity: 1; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 14px;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  opacity: 0.6;
}
.icon-btn:hover { opacity: 1; background: var(--hover-color); }

.inline-rename {
  width: 100%;
  background: var(--bg-color);
  border: 1px solid var(--accent-color);
  color: var(--text-color);
  font-size: 13px;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: inherit;
}

/* Folders */
.folder-header {
  display: flex;
  align-items: center;
  padding: 6px 8px;
  margin: 4px 0 2px 0;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s;
  gap: 6px;
}
.folder-header:hover { background: var(--hover-color); }

.folder-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  flex-shrink: 0;
  opacity: 0.6;
}
.folder-toggle svg { display: block; }

.file-icon {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  opacity: 0.5;
  margin-right: 6px;
}
.file-icon svg { display: block; }

.folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.folder-content { padding-left: 16px; }
.folder-content.collapsed { display: none; }

/* Context section */
.context-section {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
}

.context-actions {
  display: flex;
  flex-direction: column;
  gap: 0;
  margin-top: 8px;
}

.context-actions button {
  flex: none;
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 13px;
  padding: 4px 0;
  border-radius: 0;
  cursor: pointer;
  opacity: 0.55;
  text-align: left;
  transition: opacity 0.15s;
}
.context-actions button:hover { opacity: 0.85; background: none; }

/* Context actions as text links */
.context-actions-row {
  display: flex;
  flex-direction: column;
  gap: 0;
  margin-top: 8px;
  padding: 0;
}
.context-actions-row button.panel-action-link {
  flex: none;
  border: none;
  padding: 4px 0;
  font-size: 13px;
}

/* Panel footer */
.panel-footer {
  flex-shrink: 0;
  padding: 10px 16px;
  border-top: 1px solid var(--border-color);
}
.panel-settings-link {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 13px;
  cursor: pointer;
  opacity: 0.55;
  padding: 4px 0;
  font-family: inherit;
  transition: opacity 0.15s;
}
.panel-settings-link:hover { opacity: 0.85; }

.panel-footer-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
}

.panel-footer-divider {
  display: none;
}

.export-menu {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.export-menu.hidden { display: none; }

.export-menu-item {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  opacity: 0.5;
  padding: 6px 8px;
  border-radius: 4px;
  text-align: left;
  transition: all 0.15s;
}
.export-menu-item:hover { opacity: 0.9; background: var(--hover-color); }

.context-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  margin: 2px 0;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}
.context-item:hover { background: var(--hover-color); }

.context-info { flex: 1; overflow: hidden; }
.context-name { font-size: 13px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.context-name:hover { text-decoration: underline; text-decoration-style: dotted; }
.context-name-edit {
  font-size: 13px; display: block; width: 100%; border: 1px solid var(--accent-color);
  border-radius: 3px; padding: 1px 4px; background: var(--bg-color); color: var(--text-color);
  font-family: inherit; outline: none;
}
.context-meta { font-size: 10px; opacity: 0.5; cursor: pointer; }
.context-remove { flex-shrink: 0; }

.empty-message {
  font-size: 12px;
  opacity: 0.5;
  padding: 12px 8px;
  text-align: center;
  line-height: 1.6;
}

/* ===== EDITOR CONTAINER ===== */
#editor-container {
  position: fixed;
  top: 40px; left: 260px; right: 0; bottom: 0;
  overflow-y: auto;
  padding: 40px 20px;
  transition: left var(--panel-transition);
}
#editor-container.full-width { left: 0; }

#editor {
  max-width: 720px;
  margin: 0 auto;
  min-height: calc(100vh - 128px);
  font-size: 18px;
  line-height: 1.8;
  outline: none;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

#editor:empty::before {
  content: 'Start writing...';
  color: var(--placeholder-color);
  font-style: italic;
}

/* Editor content styles */
#editor p { margin-bottom: 1em; }
#editor h1 { font-size: 2em; font-weight: 600; margin: 1em 0 0.5em; }
#editor h2 { font-size: 1.5em; font-weight: 600; margin: 0.8em 0 0.4em; }
#editor h3 { font-size: 1.25em; font-weight: 600; margin: 0.6em 0 0.3em; }
#editor blockquote {
  border-left: 3px solid var(--border-color);
  padding-left: 16px;
  margin: 1em 0;
  opacity: 0.8;
}
#editor ul, #editor ol { padding-left: 24px; margin: 0.5em 0; }
#editor li { margin: 0.3em 0; }
#editor hr { border: none; border-top: 1px solid var(--border-color); margin: 2em 0; }
#editor a { color: var(--accent-color); text-decoration: underline; }
#editor code { background: var(--code-bg); padding: 2px 6px; border-radius: 3px; font-size: 0.9em; }
#editor pre {
  background: var(--code-bg);
  padding: 16px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 1em 0;
}
#editor pre code { background: none; padding: 0; }

/* ===== RIGHT PANEL (ASSISTANT) ===== */
#fieldwriter-panel {
  position: fixed;
  top: 40px; right: 0; bottom: 0;
  width: 380px;
  background: var(--bg-color);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: 60;
  transition: transform var(--panel-transition);
}
#fieldwriter-panel.hidden {
  transform: translateX(100%);
}

.fieldwriter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  gap: 8px;
}

.fieldwriter-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

#assistant-title {
  font-size: 14px;
  font-weight: 600;
}

.fieldwriter-mode-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.fieldwriter-mode-badge.plan {
  background: rgba(59, 130, 246, 0.1);
  color: #3b82f6;
  border: 1px solid rgba(59, 130, 246, 0.3);
}
.fieldwriter-mode-badge.active {
  background: rgba(16, 185, 129, 0.1);
  color: #10b981;
  border: 1px solid rgba(16, 185, 129, 0.3);
}
.fieldwriter-mode-badge:hover { opacity: 0.8; }

.fieldwriter-header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.fieldwriter-header-right button {
  background: none;
  border: none;
  color: var(--text-color);
  cursor: pointer;
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 4px;
  opacity: 0.6;
}
.fieldwriter-header-right button:hover { opacity: 1; background: var(--hover-color); }

/* Context bar */
.fieldwriter-context-bar {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border-color);
  font-size: 11px;
  opacity: 0.6;
  flex-shrink: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Messages */
.fieldwriter-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  min-height: 0;
}

.message {
  margin-bottom: 16px;
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.6;
}

.message.user {
  background: var(--msg-user-bg);
  color: var(--msg-user-color);
}

.message.assistant {
  background: transparent;
  border: 1px solid var(--border-color);
}

.message.system {
  background: var(--msg-system-bg);
  color: var(--msg-system-color);
  font-size: 12px;
  font-style: italic;
}

.message-content {
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-content p { margin: 0.5em 0; }
.message-content p:first-child { margin-top: 0; }
.message-content p:last-child { margin-bottom: 0; }
.message-content h1 { margin: 0.8em 0 0.4em; font-size: 1.3em; font-weight: 700; }
.message-content h2 { margin: 0.8em 0 0.4em; font-size: 1.2em; font-weight: 600; }
.message-content h3 { margin: 0.6em 0 0.3em; font-size: 1.1em; font-weight: 600; }
.message-content h4 { margin: 0.5em 0 0.25em; font-size: 1em; font-weight: 600; }
.message-content blockquote {
  border-left: 3px solid var(--border-color);
  padding-left: 12px;
  margin: 0.5em 0;
  opacity: 0.85;
  font-style: italic;
}
.message-content a { color: var(--accent-color); text-decoration: underline; }
.message-content a:hover { opacity: 0.8; }
.message-content code { background: var(--code-bg); padding: 2px 6px; border-radius: 3px; font-size: 0.85em; }
.message-content pre { background: var(--code-bg); padding: 12px; border-radius: 6px; overflow-x: auto; margin: 0.5em 0; }
.message-content pre code { background: none; padding: 0; }
.message-content ul, .message-content ol { padding-left: 20px; margin: 0.5em 0; }
.message-content li { margin: 0.2em 0; }
.message-content strong { font-weight: 600; }

/* Diff message */
.diff-message { border: 1px solid rgba(16, 185, 129, 0.3); }
.diff-header {
  font-size: 12px;
  font-weight: 600;
  color: #10b981;
  margin-bottom: 8px;
}
.diff-content {
  background: var(--code-bg);
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 12px;
  max-height: 300px;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.6;
}
.diff-actions {
  display: flex;
  gap: 8px;
}
.diff-accept, .diff-reject {
  flex: 1;
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: opacity 0.15s;
}
.diff-accept {
  background: #10b981;
  color: white;
}
.diff-reject {
  background: transparent;
  color: var(--text-color);
  border: 1px solid var(--border-color);
}
.diff-accept:hover, .diff-reject:hover { opacity: 0.85; }

/* Input area */
.fieldwriter-input-area {
  padding: 12px 16px 15px;
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

#assistant-input {
  flex: 1;
  background: var(--panel-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text-color);
  font-family: inherit;
  font-size: 14px;
  resize: none;
  min-height: 40px;
  max-height: 120px;
  outline: none;
}
#assistant-input:focus { border-color: var(--accent-color); }
#assistant-input::placeholder { color: var(--placeholder-color); }

#assistant-send {
  background: var(--accent-color);
  color: var(--bg-color);
  border: none;
  border-radius: 8px;
  padding: 0 16px;
  min-height: 40px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  transition: opacity 0.15s;
  align-self: flex-end;
}
#assistant-send:hover { opacity: 0.85; }

/* ===== COMMAND PALETTE ===== */
#command-palette {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 15vh;
  background: rgba(0, 0, 0, 0.4);
}
#command-palette.hidden { display: none; }

.command-palette-content {
  background: var(--bg-color);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  width: 90%;
  max-width: 520px;
  overflow: hidden;
}

#command-palette-input {
  width: 100%;
  padding: 16px 20px;
  border: none;
  background: transparent;
  color: var(--text-color);
  font-size: 16px;
  font-family: inherit;
  outline: none;
  border-bottom: 1px solid var(--border-color);
}
#command-palette-input::placeholder { color: var(--placeholder-color); }

#command-suggestions {
  max-height: 400px;
  overflow-y: auto;
  padding: 8px 0;
}

.command-category {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 8px 20px 4px;
  opacity: 0.4;
}

.command-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  cursor: pointer;
  transition: background 0.1s;
}
.command-item:hover, .command-item.selected { background: var(--hover-color); }

.command-label { font-size: 14px; }
.command-shortcut {
  font-size: 11px;
  opacity: 0.4;
  padding: 2px 8px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
}

/* ===== SETTINGS MODAL ===== */
#settings-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 5vh;
  background: rgba(0, 0, 0, 0.4);
}
#settings-modal.hidden { display: none; }

.settings-content {
  position: relative;
  background: var(--bg-color);
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  padding: 25px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
}

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

.settings-header h2 {
  font-size: 18px;
  font-weight: 600;
}

.settings-back-btn {
  background: transparent;
  border: none;
  font-size: 14px;
  color: var(--accent-color);
  cursor: pointer;
  padding: 8px 0;
}

.settings-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 20px;
  overflow-x: auto;
}

.settings-tab {
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-color);
  background: none;
  border: none;
  cursor: pointer;
  opacity: 0.6;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  transition: all 0.15s;
}
.settings-tab:hover { opacity: 0.8; }
.settings-tab.active {
  opacity: 1;
  border-bottom-color: var(--accent-color);
}

#settings-tab-content {
  min-height: 200px;
}

.settings-group {
  margin-bottom: 20px;
}

.settings-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
}

.settings-select, .settings-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-color);
  color: var(--text-color);
  font-size: 14px;
  font-family: inherit;
}
.settings-select:focus, .settings-input:focus { outline: none; border-color: var(--accent-color); }

.settings-range {
  width: 100%;
  margin-top: 4px;
}

.settings-hint {
  font-size: 11px;
  opacity: 0.5;
  margin-top: 4px;
  line-height: 1.4;
}

.settings-checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
}

.api-status {
  font-size: 12px;
  margin-top: 8px;
  opacity: 0.6;
}

.settings-btn {
  width: 100%;
  padding: 10px 16px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: transparent;
  color: var(--text-color);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.settings-btn:hover { background: var(--hover-color); }
.settings-btn.danger { color: #ef4444; border-color: #ef4444; }
.settings-btn.danger:hover { background: rgba(239, 68, 68, 0.1); }
.settings-danger { margin-top: 30px; padding-top: 20px; border-top: 1px solid var(--border-color); }

.settings-actions {
  display: flex;
  gap: 10px;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
}

.settings-actions button {
  flex: 1;
  padding: 10px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: opacity 0.15s;
}
#settings-save { background: var(--accent-color); color: var(--bg-color); }
#settings-close { background: transparent; color: var(--text-color); border: 1px solid var(--border-color); }
.settings-actions button:hover { opacity: 0.85; }

/* Theme grid */
.theme-group-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.5;
  margin: 16px 0 8px;
}
.theme-group-label:first-child { margin-top: 0; }

.theme-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.theme-card {
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
}
.theme-card:hover { border-color: var(--accent-color); }
.theme-card.active { border-color: var(--accent-color); border-width: 2px; }

.theme-preview {
  height: 48px;
  border-radius: 4px;
  margin-bottom: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

.theme-name { font-size: 12px; font-weight: 500; }

.data-stats { font-size: 13px; opacity: 0.7; }

/* ===== DOCS MODAL ===== */
#docs-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 5vh;
  background: rgba(0, 0, 0, 0.4);
}
#docs-modal.hidden { display: none; }

.docs-content {
  position: relative;
  background: var(--bg-color);
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  padding: 25px;
  width: 90%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
}

.docs-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.docs-header h2 {
  font-size: 18px;
  font-weight: 600;
}

.docs-body {
  font-size: 14px;
  line-height: 1.7;
}

.docs-section {
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-color);
}
.docs-section:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }

.docs-section h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 8px;
}

.docs-section p {
  margin: 6px 0;
  opacity: 0.85;
}

.docs-section ul, .docs-section ol {
  padding-left: 20px;
  margin: 6px 0;
}

.docs-section li {
  margin: 4px 0;
  opacity: 0.85;
}

.docs-section kbd {
  display: inline-block;
  background: var(--code-bg);
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 12px;
  font-family: 'Courier New', monospace;
  border: 1px solid var(--border-color);
}

.docs-hint {
  font-size: 12px;
  opacity: 0.6;
  margin-top: 6px;
  font-style: italic;
}

/* ===== CONTEXT PREVIEW MODAL ===== */
#context-preview-modal {
  position: fixed;
  inset: 0;
  z-index: 1500;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
}
#context-preview-modal.hidden { display: none; }

.context-preview-content {
  background: var(--bg-color);
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  padding: 24px;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.context-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.context-preview-title { font-size: 16px; font-weight: 600; }
.context-preview-close {
  background: none;
  border: none;
  color: var(--text-color);
  font-size: 20px;
  cursor: pointer;
  opacity: 0.6;
}
.context-preview-close:hover { opacity: 1; }

.context-preview-text {
  flex: 1;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  opacity: 0.8;
}

/* ===== CUSTOM DIALOGS ===== */
.custom-dialog-overlay {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0);
  transition: background 0.2s;
}
.custom-dialog-overlay.visible { background: rgba(0, 0, 0, 0.4); }

.custom-dialog {
  background: var(--bg-color);
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  padding: 24px;
  width: 90%;
  max-width: 400px;
}

.custom-dialog-message {
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 16px;
}

.custom-dialog-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-color);
  color: var(--text-color);
  font-size: 14px;
  font-family: inherit;
  margin-bottom: 16px;
}
.custom-dialog-input:focus { outline: none; border-color: var(--accent-color); }

.custom-dialog-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.custom-dialog-btn {
  padding: 8px 20px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: transparent;
  color: var(--text-color);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.custom-dialog-btn:hover { background: var(--hover-color); }
.custom-dialog-btn.primary {
  background: var(--accent-color);
  color: var(--bg-color);
  border-color: var(--accent-color);
}
.custom-dialog-btn.primary:hover { opacity: 0.85; }

/* ===== MOBILE BACKDROP ===== */
.mobile-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: calc(var(--panel-z-mobile) - 1);
  opacity: 0;
  transition: opacity var(--panel-transition);
}
.mobile-backdrop.visible { display: block; opacity: 1; }

/* ===== SCROLLBARS ===== */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(128, 128, 128, 0.5); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-color); }
* { scrollbar-width: thin; scrollbar-color: rgba(128, 128, 128, 0.5) transparent; }

/* ===== MOBILE PORTRAIT ===== */
@media (max-width: 767px) and (orientation: portrait) {
  #top-bar {
    height: 56px;
    padding: 0 12px;
  }

  #left-panel {
    width: 78%;
    max-width: 300px;
    top: 56px;
    z-index: var(--panel-z-mobile);
    border-right: 1px solid var(--border-color);
  }

  #editor-container {
    top: 56px;
    left: 0;
    padding: 20px 16px;
  }
  #editor-container.full-width { left: 0; }

  #editor {
    max-width: 100%;
    font-size: 16px;
    line-height: 1.7;
  }

  #fieldwriter-panel {
    width: 100%;
    max-width: 100%;
    top: 56px;
    z-index: calc(var(--panel-z-mobile) + 1);
  }

  body.assistant-open #top-bar { display: none !important; }
  body.assistant-open #fieldwriter-panel {
    top: 0 !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
  }
  body.assistant-open #editor-container {
    transform: translateX(-100%);
    transition: transform 0.15s ease;
  }

  .mobile-backdrop { display: none !important; }

  /* Full-width modals */
  .docs-content,
  .settings-content,
  .context-preview-content,
  .command-palette-content {
    width: 100%;
    max-width: 100vw;
    height: 100%;
    max-height: 100dvh;
    border-radius: 0;
  }

  #command-palette { padding-top: 0; }

  .settings-actions {
    flex-direction: column;
  }
  .settings-actions button,
  .custom-dialog-btn {
    min-height: var(--touch-target-size);
  }

  .file-item, .context-item {
    min-height: 44px;
    padding: 10px 12px;
  }

  #formatting-toolbar {
    top: auto !important;
    left: 0 !important;
    right: 0;
    bottom: 0;
    width: 100%;
    border-radius: 0;
    justify-content: center;
    z-index: 500;
    padding: 8px;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.08);
  }
}

/* ===== MOBILE LANDSCAPE ===== */
@media (orientation: landscape) and (max-height: 600px) {
  #top-bar { height: 44px; padding: 0 12px; }

  #left-panel { width: 200px; top: 44px; z-index: 100; }
  #left-panel.hidden { transform: translateX(-100%); }

  #fieldwriter-panel { width: 320px; max-width: 320px; top: 44px; z-index: 150; }
  #fieldwriter-panel.hidden { transform: translateX(100%); }

  #editor-container { top: 44px; left: 200px; right: 0; padding: 12px 16px; }
  #editor-container.full-width { left: 0; }

  #editor { max-width: 100%; font-size: 15px; }
}

/* ===== SAFE AREAS ===== */
@supports (padding: env(safe-area-inset-top)) {
  @media (max-width: 767px) and (orientation: portrait) {
    #top-bar {
      padding-top: env(safe-area-inset-top);
      height: calc(56px + env(safe-area-inset-top));
    }
    #left-panel, #fieldwriter-panel { top: calc(56px + env(safe-area-inset-top)); }
    #editor-container {
      top: calc(56px + env(safe-area-inset-top));
      padding-bottom: env(safe-area-inset-bottom);
    }
    .fieldwriter-input-area {
      padding-bottom: max(15px, env(safe-area-inset-bottom));
    }
  }
}

/* ===== MOBILE: INLINE RENAME PROMINENCE ===== */
@media (max-width: 767px) and (orientation: portrait) {
  .inline-rename {
    padding: 6px 10px;
    font-size: 15px;
    border-width: 2px;
    animation: rename-glow 0.6s ease;
  }
  @keyframes rename-glow {
    0%   { box-shadow: 0 0 0 0 rgba(61, 90, 122, 0.4); }
    70%  { box-shadow: 0 0 0 6px rgba(61, 90, 122, 0); }
    100% { box-shadow: 0 0 0 0 rgba(61, 90, 122, 0); }
  }
}

/* ===== TOUCH DEVICE OVERRIDES ===== */
@media (hover: none) and (pointer: coarse) {
  .clickable:hover { opacity: 1; }
  .file-item:hover:not(.active), .context-item:hover { background: transparent; }
  .file-actions, .folder-actions { opacity: 0.6; }
  .clickable:active { opacity: 0.7; }
  .file-item:active:not(.active), .context-item:active { background: var(--hover-color); }
}

/* ===== PWA INSTALL BANNER ===== */
#install-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  background: var(--accent-color);
  color: white;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
  z-index: 10000;
  font-size: 14px;
  transform: translateY(-100%);
  transition: transform 0.3s ease;
}
#install-banner.visible { transform: translateY(0); }
#install-btn {
  background: white;
  color: var(--accent-color);
  border: none;
  padding: 6px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
}
#install-dismiss {
  background: transparent;
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  padding: 0 5px;
  opacity: 0.7;
}
#install-dismiss:hover { opacity: 1; }

/* ===== WELCOME OVERLAY ===== */
#welcome-overlay {
  position: fixed;
  inset: 0;
  z-index: 5000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-color);
  transition: opacity 0.3s ease;
}
#welcome-overlay.hidden { display: none; }
#welcome-overlay.fade-out { opacity: 0; }

.welcome-content {
  text-align: center;
  max-width: 420px;
  padding: 40px 30px;
}

.welcome-title {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-bottom: 12px;
  color: var(--text-color);
}

.welcome-tagline {
  font-size: 16px;
  line-height: 1.6;
  opacity: 0.7;
  margin-bottom: 32px;
}

.welcome-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 24px;
}

.welcome-btn {
  padding: 14px 24px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  border: none;
}

.welcome-btn-primary {
  background: var(--accent-color);
  color: var(--bg-color);
}
.welcome-btn-primary:hover { opacity: 0.85; }

.welcome-btn-secondary {
  background: transparent;
  color: var(--text-color);
  border: 1px solid var(--border-color);
}
.welcome-btn-secondary:hover { background: var(--hover-color); }

.welcome-hint {
  font-size: 12px;
  opacity: 0.45;
  line-height: 1.4;
}

/* ===== OPENROUTER CONNECT BUTTON (Settings) ===== */
.openrouter-connect-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--accent-color);
  color: var(--bg-color);
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: opacity 0.15s;
}
.openrouter-connect-btn:hover { opacity: 0.85; }
.openrouter-connect-btn svg { flex-shrink: 0; }

.settings-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 0;
  font-size: 12px;
  opacity: 0.4;
}
.settings-divider::before,
.settings-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border-color);
}

/* ===== TOAST NOTIFICATIONS ===== */
#toast-container {
  position: fixed;
  top: 52px;
  right: 16px;
  z-index: 6000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  line-height: 1.4;
  max-width: 360px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  opacity: 0;
  transform: translateX(20px);
  transition: opacity 0.3s, transform 0.3s;
  pointer-events: auto;
}
.toast.visible {
  opacity: 1;
  transform: translateX(0);
}

.toast-success {
  background: #10b981;
  color: white;
}
.toast-error {
  background: #ef4444;
  color: white;
}
.toast-info {
  background: var(--accent-color);
  color: var(--bg-color);
}

/* ===== SYNC / ACCOUNT ===== */
.sync-account-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
}

.sync-icon {
  font-size: 28px;
  opacity: 0.5;
  line-height: 1;
}

.sync-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent-color);
  color: var(--bg-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  flex-shrink: 0;
}

.sync-status-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}

.sync-status-dot.sync-status-ok { background: #10b981; }
.sync-status-dot.sync-status-syncing { background: #f59e0b; animation: sync-pulse 1s infinite; }
.sync-status-dot.sync-status-offline { background: #9ca3af; }
.sync-status-dot.sync-status-error { background: #ef4444; }

.sync-status-msg {
  font-size: 12px;
  margin-top: 12px;
  min-height: 16px;
  opacity: 0.8;
}

/* Sync indicator dot in top bar */
.sync-indicator-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}

.sync-indicator-dot.sync-ok { background: #10b981; }
.sync-indicator-dot.sync-active { background: #f59e0b; animation: sync-pulse 1s infinite; }
.sync-indicator-dot.sync-error { background: #ef4444; }
.sync-indicator-dot.sync-offline { background: #9ca3af; }

@keyframes sync-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ===== PRINT ===== */
@media print {
  #top-bar, #left-panel, #fieldwriter-panel, .mobile-backdrop,
  #command-palette, #settings-modal, #docs-modal, .custom-dialog-overlay,
  #welcome-overlay, #toast-container { display: none !important; }
  #editor-container { position: static; padding: 20px; }
  #editor { max-width: 100%; }
}
