:root {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface-2: #2a2a2a;
  --accent: #d4a373;
  --text: #e8e8e8;
  --muted: #999;
  --border: #333;
}
:root.light {
  --bg: #f5f0eb;
  --surface: #ffffff;
  --surface-2: #ede8e2;
  --accent: #c9975e;
  --text: #1a1a1a;
  --muted: #888;
  --border: #d5cec6;
}

/* Login portal */
.login-portal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 3000;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 2.5rem;
  width: 340px;
  max-width: 90vw;
  text-align: center;
}
.login-card h1 {
  font-family: Georgia, 'Times New Roman', serif;
  color: var(--accent);
  margin: 0 0 0.25rem;
  font-size: 2.75rem;
  text-align: center;
  line-height: 1.15;
}
.login-subtitle {
  color: var(--muted);
  margin: 0 0 1.5rem;
  font-size: 0.85rem;
}
.login-card input {
  display: block;
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
  font-family: inherit;
  font-size: 0.95rem;
  outline: none;
  box-sizing: border-box;
}
.login-card input:focus {
  border-color: var(--accent);
}
.login-card button {
  width: 100%;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 0.5rem;
  padding: 0.75rem;
  font-family: inherit;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  margin-top: 0.25rem;
}
.login-card button:hover { opacity: 0.9; }
.login-card button:disabled { opacity: 0.5; cursor: not-allowed; }
.login-error {
  color: #ff6b6b;
  font-size: 0.85rem;
  margin-bottom: 0.75rem;
  min-height: 1.2em;
}
.login-hint {
  color: var(--muted);
  font-size: 0.8rem;
  margin: 1rem 0 0;
}

/* User badge in sidebar */
.sidebar-header-end {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.user-badge {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  color: var(--muted);
  max-width: 100px;
}
.user-badge .username {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.logout-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 0.25rem;
  padding: 0.15rem 0.4rem;
  cursor: pointer;
  font-size: 0.7rem;
  font-family: inherit;
}
.logout-btn:hover {
  color: #ff6b6b;
  border-color: #ff6b6b;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', Courier, monospace;
}
.app { display: flex; height: 100vh; }
.sidebar {
  width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem;
  overflow-y: auto;
}
.sidebar h1 {
  font-family: Georgia, 'Times New Roman', serif;
  color: var(--accent);
  margin: 0 0 1rem;
  font-size: 1.75rem;
  line-height: 1.15;
}
.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
}
#chat-header {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: bold;
}
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
}
.message {
  margin-bottom: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-left: 3px solid var(--accent);
  border-radius: 0 0.5rem 0.5rem 0;
  animation: fadeIn 0.2s ease;
}
.message.user { border-left-color: #6b8eae; }
.message.assistant { border-left-color: var(--accent); }
.message.tool {
  border-left-color: #6a9e6a;
  font-size: 0.85rem;
  color: var(--muted);
}
#composer {
  display: flex;
  padding: 1rem;
  border-top: 1px solid var(--border);
  gap: 0.5rem;
}
#composer textarea {
  flex: 1;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.75rem;
  resize: none;
}
#composer button {
  background: var(--accent);
  color: #121212;
  border: none;
  border-radius: 0.5rem;
  padding: 0 1.25rem;
  cursor: pointer;
}
#composer textarea:disabled,
#composer button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.error-toast {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: #b33;
  color: #fff;
  padding: 0.75rem 1.25rem;
  border-radius: 0.5rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 1000;
  max-width: 90vw;
}
.error-toast.visible {
  opacity: 1;
  pointer-events: auto;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
.session {
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  margin: 0.1rem 0;
  border-radius: 0.25rem;
}
.session:hover {
  background: var(--surface-2);
}
.session--active {
  background: var(--surface-2);
  border-left: 3px solid var(--accent);
  font-weight: bold;
}

/* Hierarchy tree */
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}
.sidebar-header h1 {
  margin: 0;
  font-size: 1.5rem;
}
.console-btn {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 0.25rem;
  width: 28px;
  height: 28px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.console-btn:hover {
  opacity: 0.85;
}
.tree-campaign { margin-bottom: 0.5rem; }
.tree-campaign-header {
  cursor: pointer;
  font-weight: bold;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
  color: var(--accent);
}
.tree-campaign-header:hover { background: var(--surface-2); }
.tree-adventure { margin-left: 0.75rem; }
.tree-adventure-header {
  cursor: pointer;
  padding: 0.2rem 0.5rem;
  border-radius: 0.25rem;
  color: var(--text);
  font-size: 0.9rem;
}
.tree-adventure-header:hover { background: var(--surface-2); }
.tree-modules { margin-left: 1.5rem; }
.tree-module {
  cursor: pointer;
  padding: 0.2rem 0.5rem;
  border-radius: 0.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.tree-module:hover { background: var(--surface-2); color: var(--text); }
.tree-module.session--active {
  color: var(--text);
  border-left: 2px solid var(--accent);
  font-weight: bold;
}

/* Collapsible */
.tree-adventures.collapsed,
.tree-modules.collapsed {
  display: none;
}

.sidebar-empty {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 0.5rem;
  text-align: center;
  line-height: 1.4;
}

.console-modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}
.console-modal.open { display: flex; }
.console-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}
.console-panel {
  position: relative;
  width: 800px;
  max-width: 90vw;
  max-height: 80vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  display: flex;
  flex-direction: column;
  z-index: 1;
}
.console-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: bold;
}
.console-close {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.2rem;
}
.console-close:hover { color: var(--text); }
.console-output {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 12px;
  line-height: 1.4;
  white-space: pre-wrap;
  min-height: 200px;
}
.console-input-row {
  display: flex;
  align-items: center;
  padding: 0.5rem 1rem;
  border-top: 1px solid var(--border);
  gap: 0.5rem;
}
.console-prompt { color: var(--accent); }
#console-input {
  flex: 1;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  padding: 0.5rem;
  font-family: inherit;
  font-size: 12px;
  outline: none;
}
#console-input:focus { border-color: var(--accent); }
.console-output .stdout { color: var(--text); }
.console-output .stderr { color: #ff6b6b; }
.console-output .info { color: var(--muted); }
.console-output .success { color: #51cf76; }

/* Settings panel */
.settings-btn {
  background: none;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  width: 28px;
  height: 28px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.settings-btn:hover { color: var(--text); border-color: var(--accent); }
.settings-modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}
.settings-modal.open { display: flex; }
.settings-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}
.settings-panel {
  position: relative;
  width: 420px;
  max-width: 90vw;
  max-height: 80vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  display: flex;
  flex-direction: column;
  z-index: 1;
  overflow: hidden;
}
.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: bold;
  font-size: 1.1rem;
}
.settings-close {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.2rem;
}
.settings-close:hover { color: var(--text); }
.settings-body {
  padding: 1rem;
  overflow-y: auto;
}
.settings-section { margin-bottom: 1.5rem; }
.settings-section h3 {
  margin: 0 0 0.25rem;
  font-size: 0.95rem;
  color: var(--accent);
}
.settings-hint {
  margin: 0 0 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
}
.settings-toggle {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  font-size: 0.9rem;
}
.settings-toggle input[type="checkbox"] { display: none; }
.toggle-slider {
  width: 40px;
  height: 22px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 11px;
  position: relative;
  transition: background 0.2s;
}
.toggle-slider::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  transition: transform 0.2s;
}
.settings-toggle input:checked + .toggle-slider::after { transform: translateX(18px); }
.settings-row {
  display: flex;
  gap: 0.5rem;
}
.settings-row input { flex: 1; }
.settings-field { margin-bottom: 0.5rem; }
.settings-body input[type="text"],
.settings-body input[type="password"] {
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.6rem 0.75rem;
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
  box-sizing: border-box;
}
.settings-body input:focus { border-color: var(--accent); }
.settings-action-btn {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 0.5rem;
  padding: 0.5rem 1rem;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.settings-action-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.settings-action-btn:hover:not(:disabled) { opacity: 0.9; }
.settings-message {
  min-height: 1.5em;
  font-size: 0.85rem;
  color: var(--muted);
}
.settings-message.success { color: #51cf76; }
.settings-message.error { color: #ff6b6b; }

/* Desktop: battle map fixed width */
@media (min-width: 1025px) {
  .battle-map-panel {
    width: 400px;
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
  }
  .battle-map { height: 100%; }
  .map-grid {
    flex: 1;
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 10px;
    line-height: 1.1;
    white-space: pre;
    overflow: auto;
    background: var(--surface);
    color: var(--text);
    padding: 0.5rem;
  }
  .map-toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
  }
  .map-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 0.25rem;
    width: 24px;
    height: 24px;
    cursor: pointer;
    font-size: 12px;
  }
  .map-btn:hover { background: var(--accent); color: var(--bg); }
  .coords { font-size: 11px; color: var(--muted); }
  .map-legend {
    padding: 0.5rem;
    border-top: 1px solid var(--border);
    font-size: 11px;
    color: var(--muted);
  }
}

/* Tablet: battle map as drawer */
@media (min-width: 600px) and (max-width: 1024px) {
  .app { flex-direction: row; }
  .sidebar { width: 240px; max-width: 240px; }
  .chat { flex: 1; }
  .battle-map-panel {
    position: fixed;
    top: 0;
    right: -400px;
    width: 400px;
    height: 100vh;
    background: var(--surface);
    border-left: 1px solid var(--border);
    z-index: 1500;
    transition: right 0.3s ease;
    display: flex;
    flex-direction: column;
  }
  .battle-map-panel.open { right: 0; }
  .map-grid {
    flex: 1;
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 10px;
    line-height: 1.1;
    white-space: pre;
    overflow: auto;
    background: var(--surface);
    color: var(--text);
    padding: 0.5rem;
  }
  .map-toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
  }
  .map-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 0.25rem;
    width: 24px;
    height: 24px;
    cursor: pointer;
    font-size: 12px;
  }
  .map-btn:hover { background: var(--accent); color: var(--bg); }
  .coords { font-size: 11px; color: var(--muted); }
  .map-legend {
    padding: 0.5rem;
    border-top: 1px solid var(--border);
    font-size: 11px;
    color: var(--muted);
  }
  .map-toggle {
    position: fixed;
    right: 1rem;
    top: 1rem;
    z-index: 1400;
    background: var(--accent);
    color: var(--bg);
    border: none;
    border-radius: 0.25rem;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-size: 12px;
  }
}

/* Phone: battle map as full drawer */
@media (max-width: 599px) {
  .app { flex-direction: column; position: relative; }
  .battle-map-panel {
    position: fixed;
    top: 0;
    right: -100%;
    width: 100%;
    height: 100vh;
    background: var(--surface);
    border-left: 1px solid var(--border);
    z-index: 1500;
    transition: right 0.3s ease;
    display: flex;
    flex-direction: column;
  }
  .battle-map-panel.open { right: 0; }
  .map-grid {
    flex: 1;
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 8px;
    line-height: 1.1;
    white-space: pre;
    overflow: auto;
    background: var(--surface);
    color: var(--text);
    padding: 0.5rem;
  }
  .map-toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
  }
  .map-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 0.25rem;
    width: 24px;
    height: 24px;
    cursor: pointer;
    font-size: 12px;
  }
  .map-btn:hover { background: var(--accent); color: var(--bg); }
  .coords { font-size: 11px; color: var(--muted); }
  .map-legend {
    padding: 0.5rem;
    border-top: 1px solid var(--border);
    font-size: 11px;
    color: var(--muted);
  }
  .map-toggle {
    position: fixed;
    right: 1rem;
    top: 1rem;
    z-index: 1400;
    background: var(--accent);
    color: var(--bg);
    border: none;
    border-radius: 0.25rem;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-size: 12px;
  }
  .sidebar {
    position: fixed;
    left: -260px;
    top: 0;
    width: 260px;
    height: 100vh;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem;
    overflow-y: auto;
    z-index: 1500;
    transition: left 0.3s ease;
  }
  .sidebar.open { left: 0; }
  .menu-toggle {
    position: fixed;
    left: 1rem;
    top: 1rem;
    z-index: 1400;
    background: var(--accent);
    color: var(--bg);
    border: none;
    border-radius: 0.25rem;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-size: 12px;
  }
  .chat { height: 100vh; }
}
/* ── Combat / Battle Map Styles ── */
.map-grid-container {
  flex: 1;
  overflow: hidden;
  background: #111;
  position: relative;
}
#battle-map-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: default;
}
.combat-log {
  max-height: 120px;
  overflow-y: auto;
  padding: 0.25rem 0.5rem;
  border-top: 1px solid var(--border);
  font-size: 10px;
  color: var(--muted);
  background: var(--surface-2);
}
.combat-log-entry {
  padding: 1px 0;
  line-height: 1.4;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.combat-log-entry:last-child { border-bottom: none; }
.map-btn-danger { color: #e74c3c; }
.map-btn-danger:hover { background: #e74c3c !important; color: #fff !important; }
.map-btn-close {
  color: #999;
  font-weight: bold;
  margin-left: auto;
}
.map-btn-close:hover { background: #e74c3c !important; color: #fff !important; }
.initiative-prompt {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.initiative-inner {
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: 0.5rem;
  padding: 2rem;
  max-width: 400px;
  width: 90%;
  text-align: center;
}
.initiative-inner p {
  font-size: 1.2rem;
  font-weight: bold;
  margin: 0 0 1rem;
}
.initiative-entities {
  text-align: left;
  margin: 1rem 0;
}
.initiative-entity-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.4rem 0.75rem;
  margin: 0.25rem 0;
  background: var(--surface-2);
  border-radius: 0.25rem;
  font-size: 0.85rem;
}
.initiative-roll-btn {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 0.25rem;
  padding: 0.25rem 0.75rem;
  cursor: pointer;
  font-size: 0.8rem;
}
.initiative-roll-btn:hover { opacity: 0.8; }
.initiative-timer {
  font-size: 2rem;
  font-family: monospace;
  color: var(--accent);
  margin-top: 1rem;
}
.turn-indicator {
  position: absolute;
  top: 0.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: var(--bg);
  padding: 0.25rem 1rem;
  border-radius: 1rem;
  font-size: 0.75rem;
  font-weight: bold;
  z-index: 50;
  white-space: nowrap;
}
