/* ───────────────────────────────────────────────
   Reset & tokens
──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #f8f7f4;
  --surface:     #ffffff;
  --border:      #e2ddd8;
  --text:        #1a1a1a;
  --text-muted:  #6b6560;
  --accent:      #6c47ff;
  --accent-dark: #5236cc;
  --radius:      12px;
  --shadow:      0 2px 12px rgba(0,0,0,.08);
  --header-h:    56px;
}

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ───────────────────────────────────────────────
   Header
──────────────────────────────────────────────── */
header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 10;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -.02em;
  color: var(--accent);
}

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

/* ───────────────────────────────────────────────
   API Key button
──────────────────────────────────────────────── */
.btn-key {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: .9rem;
  cursor: pointer;
  color: var(--text-muted);
  transition: border-color .15s, color .15s;
  line-height: 1;
}

.btn-key:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-key.has-key {
  border-color: #16a34a;
  color: #16a34a;
}

/* ───────────────────────────────────────────────
   API Key panel
──────────────────────────────────────────────── */
.key-panel {
  position: fixed;
  top: var(--header-h);
  right: 0;
  left: 0;
  z-index: 9;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0,0,0,.1);
}

.key-panel-inner {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.key-panel-inner label {
  font-size: .8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
}

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

.key-row input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .9rem;
  font-family: monospace;
  outline: none;
  transition: border-color .15s;
}

.key-row input:focus {
  border-color: var(--accent);
}

.btn-save-key {
  padding: 8px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: .875rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s;
}

.btn-save-key:hover { background: var(--accent-dark); }

.btn-clear-key {
  padding: 8px 16px;
  background: none;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .875rem;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color .15s, color .15s;
}

.btn-clear-key:hover {
  border-color: #dc2626;
  color: #dc2626;
}

.key-hint {
  font-size: .78rem;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ───────────────────────────────────────────────
   Tab bar (mobile only)
──────────────────────────────────────────────── */
.tab-bar {
  display: none;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}

.tab {
  padding: 5px 18px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.tab.active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,.12);
}

/* ───────────────────────────────────────────────
   Mode selector (header, desktop only)
──────────────────────────────────────────────── */
.mode-selector {
  display: flex;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}

.mode-btn {
  padding: 4px 14px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: .8rem;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.mode-btn.active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,.12);
}

/* ───────────────────────────────────────────────
   Main layout
──────────────────────────────────────────────── */
.app {
  display: flex;
  height: calc(100vh - var(--header-h));
  margin-top: var(--header-h);
}

/* ───────────────────────────────────────────────
   Panes
──────────────────────────────────────────────── */
.pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
}

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

.pane-header-ai:not(:has(.cache-badge:not(.hidden))) {
  display: none;
}

.pane-title {
  font-size: .8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
}

/* ───────────────────────────────────────────────
   Divider (desktop)
──────────────────────────────────────────────── */
.divider {
  width: 1px;
  background: var(--border);
  flex-shrink: 0;
}

/* ───────────────────────────────────────────────
   Samples dropdown
──────────────────────────────────────────────── */
.samples-container {
  position: relative;
}

.btn-samples {
  padding: 5px 12px;
  background: none;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .15s, color .15s;
  white-space: nowrap;
}

.btn-samples:hover,
.btn-samples.open {
  border-color: var(--accent);
  color: var(--accent);
}

.samples-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 100;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
  min-width: 280px;
  max-height: 420px;
  overflow-y: auto;
  padding: 8px 0;
}

.samples-group-label {
  padding: 8px 16px 4px;
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
}

.samples-item {
  display: block;
  width: 100%;
  padding: 8px 16px;
  text-align: left;
  background: none;
  border: none;
  font-size: .875rem;
  color: var(--text);
  cursor: pointer;
  transition: background .12s;
  line-height: 1.4;
}

.samples-item:hover {
  background: var(--bg);
  color: var(--accent);
}

.samples-divider {
  height: 1px;
  background: var(--border);
  margin: 6px 0;
}

/* ───────────────────────────────────────────────
   Clear button (in header)
──────────────────────────────────────────────── */
.btn-clear {
  padding: 5px 12px;
  background: none;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .15s, color .15s;
  white-space: nowrap;
}

.btn-clear:hover {
  border-color: #dc2626;
  color: #dc2626;
}

/* ───────────────────────────────────────────────
   Textarea
──────────────────────────────────────────────── */
textarea {
  flex: 1;
  padding: 20px;
  border: none;
  outline: none;
  resize: none;
  font-size: 1rem;
  line-height: 1.7;
  color: var(--text);
  background: transparent;
  font-family: inherit;
}

textarea::placeholder { color: var(--text-muted); }

/* ───────────────────────────────────────────────
   AI content area
──────────────────────────────────────────────── */
.ai-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  font-size: 1rem;
  line-height: 1.7;
  color: var(--text);
}

.ai-content p { margin-bottom: 1em; }
.ai-content p:last-child { margin-bottom: 0; }

.placeholder {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-muted);
  padding: 40px;
}

.placeholder p { font-size: .95rem; max-width: 280px; line-height: 1.6; }

/* ───────────────────────────────────────────────
   Cache badge
──────────────────────────────────────────────── */
.cache-badge {
  font-size: .75rem;
  font-weight: 600;
  color: #16a34a;
  background: #dcfce7;
  padding: 3px 9px;
  border-radius: 99px;
}

.hidden { display: none !important; }

/* ───────────────────────────────────────────────
   Loading state
──────────────────────────────────────────────── */
.loading {
  position: absolute;
  inset: var(--header-h) 0 0 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--text-muted);
  font-size: .9rem;
  background: var(--surface);
  z-index: 5;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ───────────────────────────────────────────────
   Error state
──────────────────────────────────────────────── */
.error-msg {
  color: #dc2626;
  background: #fee2e2;
  border-radius: 8px;
  padding: 12px 16px;
  font-size: .9rem;
}

/* ───────────────────────────────────────────────
   Mobile: single pane + swipe
──────────────────────────────────────────────── */
@media (max-width: 700px) {
  :root { --header-h: 120px; }

  header {
    flex-wrap: wrap;
    align-content: center;
    padding: 8px 16px;
    gap: 0;
    row-gap: 8px;
  }

  header h1 {
    width: 100%;
    font-size: .95rem;
  }

  .header-right {
    width: 100%;
    flex-wrap: wrap;
    row-gap: 8px;
  }

  .samples-menu {
    left: 0;
    right: auto;
    max-width: calc(100vw - 32px);
  }

  .tab-bar {
    display: flex;
    order: -1;
    width: 100%;
  }

  .tab { flex: 1; text-align: center; }

  .divider { display: none; }
  .mode-selector { display: none; }

  .app {
    position: relative;
    overflow: hidden;
  }

  .pane {
    position: absolute;
    inset: 0;
    width: 100%;
    transform: translateX(100%);
    transition: transform .3s cubic-bezier(.4,0,.2,1);
    will-change: transform;
  }

  .pane.active { transform: translateX(0); }

  /* Loading overlay in mobile: center on active pane */
  .loading {
    left: 0;
  }
}

/* ───────────────────────────────────────────────
   Desktop: left pane starts visible
──────────────────────────────────────────────── */
@media (min-width: 701px) {
  .pane { position: relative; }
  .pane-text { display: flex; }
  .pane-ai { display: flex; }
}
