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

:root {
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --primary: #e94560;
  --secondary: #2d2d2d;
  --text: #eaeaea;
  --muted: #888888;
  --border: #404040;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

.login-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  z-index: 9999;
  justify-content: center;
  align-items: center;
}

.login-overlay.show {
  display: flex;
}

.login-box {
  background: var(--surface);
  padding: 40px;
  border-radius: 12px;
  text-align: center;
  border: 2px solid var(--primary);
}

.login-box h2 {
  margin-bottom: 20px;
  color: var(--primary);
}

.login-box input {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 4px;
  margin-bottom: 16px;
}

.login-error {
  color: var(--primary);
  margin-top: 12px;
  font-size: 14px;
}

.login-box button {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

.login-box button:hover {
  opacity: 0.9;
}

header {
  background: var(--surface);
  padding: 16px 24px;
  border-bottom: 2px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 10;
}

h1 {
  font-size: 24px;
  font-weight: bold;
  color: var(--primary);
}

.status {
  font-size: 14px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

.toggle-btn {
  position: fixed;
  top: 80px;
  width: 36px;
  height: 36px;
  background: var(--primary);
  border: none;
  border-radius: 4px;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  font-size: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  user-select: none;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.toggle-btn:hover {
  transform: scale(1.05);
  opacity: 0.9;
}

#library-toggle {
  left: 16px;
}

#settings-toggle {
  right: 16px;
}

.refresh-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 4px;
}

.refresh-btn:hover {
  background: var(--secondary);
  color: var(--text);
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
}

.status-dot.connected { background: #2ecc71; }
.status-dot.error { background: var(--primary); }

main {
  display: flex;
  height: 100%;
  min-height: calc(100vh - 70px);
  position: relative;
}

.panel {
  background: var(--surface);
  padding: 20px;
  overflow-y: auto;
  z-index: 1;
  box-sizing: border-box;
}

.panel.left-panel {
  width: 280px;
  flex-shrink: 0;
  border-right: 2px solid var(--secondary);
  transition: width 0.3s ease, opacity 0.3s ease;
  padding-top: 36px;
}

.panel.right-panel {
  width: 350px;
  flex-shrink: 0;
  border-left: 2px solid var(--secondary);
  transition: width 0.3s ease, opacity 0.3s ease;
}

.panel.right-panel label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: var(--muted);
  font-weight: bold;
}

.panel.right-panel textarea,
.panel.right-panel input[type="text"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--secondary);
  border-radius: 4px;
  padding: 10px;
  color: var(--text);
  font-family: 'Courier New', monospace;
  font-size: 14px;
  margin-bottom: 16px;
  resize: vertical;
}

.panel.right-panel textarea:focus,
.panel.right-panel input[type="text"]:focus {
  outline: none;
  border-color: var(--primary);
}

.panel.right-panel select {
  width: 100%;
  padding: 10px;
  background: var(--bg);
  border: 1px solid var(--secondary);
  border-radius: 4px;
  color: var(--text);
  font-size: 14px;
  margin-bottom: 16px;
}

.panel.hidden {
  width: 60px;
  padding: 10px;
  opacity: 1;
  overflow: hidden;
}

.panel.hidden > * {
  visibility: hidden;
}

.panel.hidden::after {
  content: '';
  display: block;
}

.center-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 24px;
  padding-bottom: 48px;
  width: auto;
  min-width: 0;
  gap: 8px;
  max-width: 100%;
  overflow: hidden;
}

.panel.left-panel.hidden {
  width: 60px;
  min-width: 60px;
}

.panel.right-panel.hidden {
  width: 60px;
  min-width: 60px;
}

.center-panel label {
  margin-bottom: 0;
}

.style-controls {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
  justify-content: center;
  align-items: center;
  padding: 8px;
  background: var(--surface);
  border-radius: 6px;
  border: 1px solid var(--secondary);
}

.contenteditable-editor {
  flex: 1;
  min-height: 200px;
  max-height: 60vh;
  font-family: 'Courier New', monospace;
  font-size: 16px;
  line-height: 1.6;
  background: var(--bg);
  color: var(--text);
  padding: 12px;
  border: 1px solid var(--secondary);
  border-radius: 4px;
  overflow-y: auto;
  outline: none;
  white-space: pre-wrap;
}

.contenteditable-editor[contenteditable="true"] {
  cursor: text;
}

.contenteditable-editor:focus {
  border-color: var(--primary);
}

.contenteditable-editor strong {
  font-weight: bold;
  color: var(--primary);
}

.contenteditable-editor em {
  font-style: italic;
}

.contenteditable-editor blockquote {
  border-left: 4px solid var(--primary);
  margin-left: 0;
  padding-left: 12px;
  color: #aaa;
  font-style: italic;
}

.contenteditable-editor h1 { font-size: 2em; margin: 0.67em 0; }
.contenteditable-editor h2 { font-size: 1.5em; margin: 0.75em 0; }
.contenteditable-editor h3 { font-size: 1.17em; margin: 0.83em 0; }

.title-input {
  width: 100%;
  font-size: 28px;
  font-weight: bold;
  background: transparent;
  border: none;
  border-bottom: 2px solid var(--secondary);
  color: var(--text);
  padding: 8px 0;
  margin-bottom: 8px;
}

.title-input:focus {
  outline: none;
  border-bottom-color: var(--primary);
}

.slider-container {
  margin-bottom: 16px;
}

.slider-container label {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.slider-container label span {
  font-weight: bold;
  color: var(--primary);
}

input[type="range"] {
  width: 100%;
  margin-top: 8px;
  accent-color: var(--primary);
}

.font-tabs, .size-tabs, .bg-tabs {
  display: flex;
  gap: 4px;
  background: var(--secondary);
  padding: 4px;
  border-radius: 6px;
}

.font-tab, .size-tab, .bg-tab {
  background: transparent;
  border: none;
  color: var(--text);
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.2s;
}

.font-tab:hover, .size-tab:hover, .bg-tab:hover {
  background: var(--border);
}

.font-tab.active, .size-tab.active, .bg-tab.active {
  background: var(--primary);
  color: white;
}

.memory-textarea {
  min-height: 80px !important;
}

select {
  width: 100%;
  padding: 10px;
  background: var(--bg);
  border: 1px solid var(--secondary);
  border-radius: 4px;
  color: var(--text);
  font-size: 14px;
  margin-bottom: 16px;
}

select:focus {
  outline: none;
  border-color: var(--primary);
}

.error-msg {
  background: rgba(233, 69, 96, 0.2);
  border: 1px solid var(--primary);
  padding: 12px;
  border-radius: 4px;
  margin-top: 16px;
  display: none;
}

.error-msg.show {
  display: block;
}

.bg-tab {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  padding: 0;
  border: 2px solid transparent;
}

.bg-tab.active {
  border-color: var(--primary);
}

.size-tab {
  font-size: 11px;
}

.size-tab[data-size="14px"] { font-size: 11px; }
.size-tab[data-size="16px"] { font-size: 14px; }
.size-tab[data-size="18px"] { font-size: 17px; }
.size-tab[data-size="22px"] { font-size: 20px; }
.size-tab[data-size="26px"] { font-size: 24px; }

.new-story-btn {
  width: 100%;
  padding: 10px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  margin-bottom: 16px;
}

.new-story-btn:hover {
  opacity: 0.9;
}

.btn {
  background: var(--primary);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  margin-top: 16px;
  width: 100%;
  transition: opacity 0.2s;
}

.btn:hover {
  opacity: 0.9;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn.secondary {
  background: var(--secondary);
  color: var(--text);
}

.btn.secondary:hover {
  background: #444444;
}

.btn.danger {
  background: #8b0000;
  color: white;
}

.btn.danger:hover {
  background: #a00000;
}

.btn.small {
  padding: 8px 12px;
  font-size: 13px;
  width: auto;
  flex: 1;
}

.library-item {
  background: var(--bg);
  border: 1px solid var(--secondary);
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: border-color 0.2s;
}

.library-item:hover {
  border-color: var(--primary);
}

.library-item-title {
  font-weight: bold;
  font-size: 16px;
  color: var(--text);
  margin-bottom: 4px;
}

.library-item-summary {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.library-item-date {
  font-size: 11px;
  color: var(--secondary);
}

.settings-buttons {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.button-row {
  display: flex;
  gap: 8px;
}

.button-row .btn {
  flex: 1;
  margin-top: 0;
}

/* System prompt editor */
.system-prompt-section {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--secondary);
}

.system-prompt-section textarea {
  width: 100%;
  min-height: 120px;
  background: var(--bg);
  border: 1px solid var(--secondary);
  border-radius: 4px;
  padding: 10px;
  color: var(--text);
  font-family: 'Courier New', monospace;
  font-size: 13px;
  margin-bottom: 8px;
  resize: vertical;
}

.system-prompt-section textarea:focus {
  outline: none;
  border-color: var(--primary);
}

.system-prompt-save {
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}

/* Seed toggle */
.seed-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  font-size: 13px;
}

.seed-toggle .checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  color: var(--muted);
}

.seed-toggle input[type="checkbox"] {
  accent-color: var(--primary);
  cursor: pointer;
}

.seed-value {
  font-family: 'Courier New', monospace;
  font-size: 12px;
  color: var(--primary);
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
}

/* Token budget display */
.token-budget {
  margin-bottom: 16px;
}

.token-budget-bar {
  width: 100%;
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid var(--secondary);
}

.token-budget-fill {
  height: 100%;
  width: 0%;
  background: #2ecc71;
  border-radius: 4px;
  transition: width 0.3s ease, background 0.3s ease;
}

.token-budget-fill.warning {
  background: #f39c12;
}

.token-budget-fill.danger {
  background: var(--primary);
}

.token-budget-text {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
  font-family: 'Courier New', monospace;
}

/* Collapsible sections */
.collapsible-section {
  margin-bottom: 12px;
}

.collapsible-toggle {
  width: 100%;
  padding: 8px 12px;
  background: var(--secondary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  transition: background 0.2s;
}

.collapsible-toggle:hover {
  background: var(--border);
}

.collapsible-toggle::after {
  content: ' ▼';
  float: right;
  font-size: 10px;
}

.collapsible-toggle.active::after {
  content: ' ▲';
}

.collapsible-content {
  padding: 8px 0;
}

.template-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.template-btn {
  background: var(--bg);
  border: 1px solid var(--secondary);
  border-radius: 4px;
  color: var(--text);
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.template-btn:hover {
  border-color: var(--primary);
  background: rgba(233, 69, 96, 0.1);
}

/* Token counters */
.token-counter {
  font-size: 11px;
  color: var(--muted);
  font-family: 'Courier New', monospace;
  margin-top: -12px;
  margin-bottom: 12px;
  text-align: right;
}

/* Diff highlight for newly generated text */
.diff-highlight {
  background: rgba(233, 69, 96, 0.15);
  transition: background 3s ease;
}

.diff-highlight.fade {
  background: transparent;
}
