:root {
  --bg-deep: #050805;
  --bg-panel: #070c09;
  --bg-panel-2: #000000;
  --parchment: #e9e6da;
  --parchment-dim: #a9c2ab;
  --gold: #c9a227;
  --gold-bright: #e8c860;
  --blood: #8a1f2b;
  --blood-bright: #c9384a;
  --border: #24402c;
  --radius: 10px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: radial-gradient(circle at 20% -10%, #17301f 0%, #081109 55%, #020402 100%);
  color: var(--parchment);
  font-family: "EB Garamond", Georgia, serif;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: linear-gradient(180deg, #000000, #060c07);
  border-bottom: 2px solid var(--gold);
  box-shadow: var(--shadow);
  flex-wrap: wrap;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  font-size: 34px;
  filter: drop-shadow(0 0 6px rgba(200, 160, 40, 0.6));
}

.brand h1 {
  font-family: "Cinzel Decorative", "Uncial Antiqua", serif;
  font-size: 22px;
  margin: 0;
  color: var(--gold-bright);
  letter-spacing: 1px;
  text-shadow: 0 0 10px rgba(200, 160, 40, 0.35);
}

.subtitle {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--parchment-dim);
  font-style: italic;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.status-pill {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #122016;
}
.status-unknown { color: #a9c2ab; }
.status-ok { color: #7be08a; border-color: #2e5c34; background: #17301b; }
.status-error { color: #ff8080; border-color: #5c2e2e; background: #301717; }

/* ---------- Buttons ---------- */
.btn {
  font-family: "EB Garamond", Georgia, serif;
  font-size: 14px;
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid var(--border);
  transition: transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }

.btn.ghost {
  background: #102618;
  color: var(--parchment-dim);
}
.btn.ghost:hover {
  background: #163a20;
  color: var(--gold-bright);
}

.btn.primary {
  background: linear-gradient(180deg, var(--gold-bright), var(--gold));
  color: #241a05;
  font-weight: 600;
  border: 1px solid #8a6a10;
  box-shadow: 0 2px 10px rgba(201, 162, 39, 0.3);
}
.btn.primary:hover {
  box-shadow: 0 4px 14px rgba(201, 162, 39, 0.5);
}

.btn.full { width: 100%; }

.btn.small {
  padding: 4px 10px;
  font-size: 12px;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  display: flex;
  gap: 18px;
  padding: 18px 24px;
  overflow: hidden;
}

.sidebar {
  width: 300px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  padding-right: 4px;
}

.panel {
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-panel-2));
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 12px;
  font-family: "Cinzel Decorative", serif;
  font-size: 16px;
  color: var(--gold-bright);
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}

/* ---------- Character form ---------- */
.character-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.character-form label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--parchment-dim);
  gap: 4px;
}
.grid-2 {
  display: grid;
  grid-template-columns: 70px 1fr 1fr;
  gap: 10px;
}

/* ---------- Multiclass block ---------- */
.classes-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.classes-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--parchment-dim);
}
.class-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.class-row {
  display: grid;
  grid-template-columns: 2fr 1fr auto;
  gap: 8px;
  align-items: center;
}
.class-row input {
  width: 100%;
}
.remove-class-btn {
  background: #0c150e;
  color: var(--blood-bright);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  flex-shrink: 0;
}
.remove-class-btn:hover {
  background: #2a1418;
  border-color: var(--blood);
}
.remove-class-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}
input, textarea {
  font-family: "EB Garamond", Georgia, serif;
  background: #0c150e;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--parchment);
  padding: 8px 10px;
  font-size: 14px;
  resize: vertical;
}
input:focus, textarea:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(201, 162, 39, 0.2);
}

.hint {
  font-size: 12px;
  color: var(--parchment-dim);
}
.hidden { display: none !important; }

/* ---------- Dice tray ---------- */
.dice-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.die {
  background: #0c150e;
  color: var(--gold-bright);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 0;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.die:hover {
  background: #163a20;
  transform: translateY(-1px);
}
.dice-result {
  margin-top: 12px;
  text-align: center;
  font-family: "Cinzel Decorative", serif;
  font-size: 20px;
  color: var(--parchment);
  min-height: 28px;
}

/* ---------- Chat panel ---------- */
.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-panel-2));
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  background-image:
    radial-gradient(circle at 50% 0%, rgba(201, 162, 39, 0.05), transparent 60%);
}

.scene-message {
  align-self: center;
  max-width: 640px;
  text-align: center;
  font-style: italic;
  color: var(--parchment-dim);
  padding: 12px 18px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.02);
}

.message {
  max-width: 75%;
  padding: 12px 16px;
  border-radius: 14px;
  line-height: 1.5;
  font-size: 15px;
  white-space: pre-wrap;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.message.user {
  align-self: flex-end;
  background: linear-gradient(180deg, #1c3323, #12241a);
  border: 1px solid var(--blood);
  border-bottom-right-radius: 3px;
}

.message.assistant {
  align-self: flex-start;
  background: linear-gradient(180deg, #14261a, #0d1810);
  border: 1px solid var(--border);
  border-bottom-left-radius: 3px;
}

.message .role-label {
  display: block;
  font-family: "Cinzel Decorative", serif;
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  margin-bottom: 6px;
}
.message.user .role-label { color: var(--blood-bright); }
.message.assistant .role-label { color: var(--gold-bright); }

.message.error {
  align-self: center;
  border: 1px solid var(--blood);
  background: #2a1418;
  color: #ffb3b3;
}

.message.speaking {
  box-shadow: 0 0 0 2px var(--gold), 0 2px 8px rgba(0, 0, 0, 0.25);
}

.message-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.speak-btn {
  background: transparent;
  border: none;
  color: var(--parchment-dim);
  cursor: pointer;
  font-size: 13px;
  padding: 0 2px;
  line-height: 1;
}
.speak-btn:hover {
  color: var(--gold-bright);
}

/* ---------- Chat images ---------- */
.chat-image-link {
  display: block;
  margin-top: 8px;
}
.chat-image {
  max-width: 100%;
  max-height: 360px;
  width: auto;
  height: auto;
  border-radius: 10px;
  border: 1px solid var(--border);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  display: block;
  cursor: zoom-in;
  background: #0c150e;
}
.chat-image:hover {
  border-color: var(--gold);
}

.message-images {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}
.message-images.inline {
  margin: 8px 0;
}
.message-text + .message-text {
  margin-top: 9px;
}
.message-images .chat-image-link {
  margin-top: 0;
}
.chat-image-link .chat-image {
  cursor: zoom-in;
}

/* ---------- Image lightbox (in-page viewer) ---------- */
.image-lightbox {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 32px;
  background: rgba(4, 10, 6, 0.88);
  backdrop-filter: blur(3px);
  animation: lightbox-fade 0.18s ease-out;
}
.image-lightbox.hidden {
  display: none;
}
@keyframes lightbox-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
.image-lightbox img {
  max-width: min(92vw, 1100px);
  max-height: calc(100vh - 130px);
  object-fit: contain;
  border: 1px solid var(--gold);
  border-radius: 6px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.6);
  background: #0c150e;
}
.image-lightbox-close {
  position: absolute;
  top: 14px;
  right: 18px;
  width: 40px;
  height: 40px;
  border: 1px solid var(--gold);
  border-radius: 50%;
  background: rgba(12, 21, 14, 0.9);
  color: var(--gold);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}
.image-lightbox-close:hover {
  background: rgba(212, 175, 55, 0.2);
  transform: scale(1.06);
}
.image-lightbox-open {
  color: var(--gold);
  font-size: 0.85rem;
  text-decoration: none;
  border-bottom: 1px dotted var(--gold);
  padding-bottom: 1px;
}
.image-lightbox-open:hover {
  opacity: 0.85;
}
body.lightbox-open {
  overflow: hidden;
}

/* ---------- First-time sign-in gate ---------- */
.sign-in-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: #000;
}
.sign-in-overlay.hidden {
  display: none;
}
.sign-in-card {
  width: 100%;
  max-width: 470px;
  max-height: 92vh;
  overflow-y: auto;
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-panel-2));
  border: 1px solid var(--gold);
  border-radius: 10px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.9);
  padding: 26px 26px 20px;
  animation: sign-in-rise 0.25s ease-out;
}
@keyframes sign-in-rise {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: translateY(0); }
}
.sign-in-title {
  margin: 0 0 4px;
  font-size: 1.5rem;
  color: var(--gold);
}
.sign-in-sub {
  margin: 0 0 14px;
  color: var(--parchment-dim);
  font-size: 0.92rem;
}
.sign-in-label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 14px 0 0;
  font-size: 0.85rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--parchment-dim);
}
.sign-in-label input {
  width: 100%;
  font-size: 15px;
  padding: 9px 11px;
}
.sign-in-label input:focus {
  outline: none;
  border-color: var(--gold);
}
.sign-in-help {
  margin-top: 14px;
  padding: 12px 14px;
  border: 1px dashed rgba(212, 175, 55, 0.4);
  border-radius: 8px;
  font-size: 0.85rem;
  color: var(--parchment-dim);
  line-height: 1.5;
}
.sign-in-help strong {
  color: var(--gold);
}
.sign-in-help ol {
  margin: 6px 0 0;
  padding-left: 20px;
}
.sign-in-help li {
  margin: 3px 0;
}
.sign-in-message {
  margin: 12px 0 0;
  font-size: 0.9rem;
}
.sign-in-btn {
  width: 100%;
  margin-top: 16px;
}
.sign-in-note {
  margin: 10px 0 0;
  text-align: center;
  font-size: 0.8rem;
  font-style: italic;
  color: var(--parchment-dim);
}
body.sign-in-open {
  overflow: hidden;
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 24px 12px;
  color: var(--parchment-dim);
  font-size: 13px;
  font-style: italic;
}
.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--gold);
  animation: bounce 1.2s infinite ease-in-out;
}
.dot:nth-child(2) { animation-delay: 0.15s; }
.dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-5px); opacity: 1; }
}

/* Chat input */
.chat-input-form {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: #081109;
}
.chat-input-form textarea {
  flex: 1;
  resize: none;
}
.send-btn {
  align-self: flex-end;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(5, 3, 10, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-content {
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-panel-2));
  border: 1px solid var(--gold);
  border-radius: 14px;
  padding: 24px;
  width: 90%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.6);
  max-height: 90vh;
  overflow-y: auto;
}
.modal-content h2 {
  margin: 0 0 6px;
  font-family: "Cinzel Decorative", serif;
  color: var(--gold-bright);
}
.modal-content label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--parchment-dim);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
  flex-wrap: wrap;
}

/* Scrollbars */
.chat-log::-webkit-scrollbar, .sidebar::-webkit-scrollbar, .modal-content::-webkit-scrollbar {
  width: 8px;
}
.chat-log::-webkit-scrollbar-thumb, .sidebar::-webkit-scrollbar-thumb, .modal-content::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* Responsive */
@media (max-width: 900px) {
  .layout {
    flex-direction: column;
    overflow: auto;
  }
  .sidebar {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .sidebar .panel { flex: 1; min-width: 260px; }
  .message { max-width: 90%; }
}
