* { box-sizing: border-box; }

html, body {
  min-height: 100%;
}

body {
  font-family: system-ui, sans-serif;
  background: #1a1a1e;
  color: #e8e8ea;
  margin: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

#topbar {
  position: sticky;
  top: 0;
  z-index: 5;
}

header {
  background: #1a1a1e;
  padding: calc(0.75rem + env(safe-area-inset-top)) calc(1rem + env(safe-area-inset-right)) 0.75rem calc(1rem + env(safe-area-inset-left));
  border-bottom: 1px solid #333;
}

header h1 {
  margin: 0;
  font-size: 1.15rem;
}

.subtitle {
  margin: 0.25rem 0 0;
  color: #9a9aa2;
  font-size: 0.9rem;
}

#controls {
  background: #232329;
  border-bottom: 1px solid #333;
  font-size: 0.9rem;
}

#controls summary {
  padding: 0.6rem calc(1rem + env(safe-area-inset-right)) 0.6rem calc(1rem + env(safe-area-inset-left));
  cursor: pointer;
  color: #c8c8ce;
  list-style: none;
}

#controls summary::-webkit-details-marker {
  display: none;
}

#controls summary::before {
  content: "▸ ";
}

#controls[open] summary::before {
  content: "▾ ";
}

.controls-grid {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0 calc(1rem + env(safe-area-inset-right)) 1rem calc(1rem + env(safe-area-inset-left));
}

.controls-grid label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.controls-grid .checkbox-label {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

.dialect-fieldset {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  border: 1px solid #444;
  border-radius: 8px;
  padding: 0.5rem 0.75rem 0.75rem;
  margin: 0;
}

.dialect-fieldset legend {
  padding: 0 0.3rem;
  font-size: 0.85rem;
  color: #c8c8ce;
}

.controls-grid select {
  width: 100%;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid #444;
  background: #17171b;
  color: #e8e8ea;
  font-size: 1rem;
}

.controls-grid input[type="checkbox"] {
  width: 1.2rem;
  height: 1.2rem;
}

#chat {
  flex: 1;
  padding: 1rem calc(1rem + env(safe-area-inset-right)) 1rem calc(1rem + env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.msg {
  position: relative;
  max-width: 88%;
  padding: 0.6rem 3.4rem 0.6rem 0.85rem;
  border-radius: 10px;
  line-height: 1.4;
  opacity: 1;
  transform: translateY(0);
}

.msg-text {
  display: block;
  white-space: pre-wrap;
  word-wrap: break-word;
  cursor: text;
}

.msg-edit {
  display: block;
  width: 100%;
  resize: none;
  border: none;
  outline: none;
  padding: 0;
  margin: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  line-height: inherit;
  white-space: pre-wrap;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.35);
  border-radius: 6px;
}

.msg-actions {
  position: absolute;
  top: 0.3rem;
  right: 0.3rem;
  display: flex;
  gap: 0.3rem;
}

.msg-action-btn {
  width: 1.4rem;
  height: 1.4rem;
  min-height: 0;
  padding: 0;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
}

.msg-action-btn:hover {
  background: rgba(255, 255, 255, 0.32);
}

.msg.user {
  align-self: flex-end;
  background: #3a5ba0;
}

.msg.assistant {
  align-self: flex-start;
  background: #2e2e35;
}

.msg.msg-fade-in {
  animation: msg-fade-in 0.25s ease;
}

@keyframes msg-fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 0.2rem 0;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #9a9aa2;
  animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.msg-debug {
  align-self: flex-start;
  max-width: 88%;
  width: 100%;
}

.msg-debug summary {
  cursor: pointer;
  color: #9a9aa2;
  font-size: 0.8rem;
}

.msg-debug pre {
  background: #111114;
  padding: 0.6rem;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.75rem;
  max-height: 300px;
}

.controls-note {
  margin: 0;
  font-size: 0.8rem;
  color: #9a9aa2;
}

#composer {
  position: sticky;
  bottom: 0;
  z-index: 5;
  background: #232329;
  border-top: 1px solid #333;
}

#chat-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.5rem calc(1rem + env(safe-area-inset-right)) 0 calc(1rem + env(safe-area-inset-left));
}

#chat-toolbar button {
  padding: 0.4rem 0.7rem;
  min-height: 0;
  font-size: 0.8rem;
  background: #2e2e35;
  border: 1px solid #444;
}

#chat-toolbar button:hover {
  background: #3a3a42;
}

#chat-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem calc(1rem + env(safe-area-inset-right)) calc(0.75rem + env(safe-area-inset-bottom)) calc(1rem + env(safe-area-inset-left));
}

#chat-input {
  flex: 1;
  min-width: 0;
  padding: 0.65rem 0.8rem;
  border-radius: 8px;
  border: 1px solid #444;
  background: #17171b;
  color: #e8e8ea;
  font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
}

button {
  padding: 0.65rem 1rem;
  border-radius: 8px;
  border: none;
  background: #3a5ba0;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  min-height: 44px;
}

button:hover {
  background: #4a6bb0;
}

button:disabled {
  opacity: 0.6;
  cursor: default;
}

.gate-body {
  align-items: center;
  justify-content: center;
  padding: calc(1rem + env(safe-area-inset-top)) calc(1rem + env(safe-area-inset-right)) calc(1rem + env(safe-area-inset-bottom)) calc(1rem + env(safe-area-inset-left));
}

.gate-box {
  background: #232329;
  border: 1px solid #333;
  border-radius: 10px;
  padding: 1.5rem;
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.gate-box .question {
  margin: 1.1rem 0 0.5rem;
  font-size: 1rem;
}

.gate-box form {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.gate-box input {
  padding: 0.65rem 0.8rem;
  border-radius: 8px;
  border: 1px solid #444;
  background: #17171b;
  color: #e8e8ea;
  font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
  text-align: center;
}

.gate-box button {
  margin-top: 0.5rem;
}

.gate-error {
  color: #e07a7a;
  margin-top: 1rem;
}

/* Wider screens: open up the layout a bit */
@media (min-width: 700px) {
  header {
    padding: 1rem 1.5rem 0.5rem;
  }

  header h1 {
    font-size: 1.4rem;
  }

  #controls summary,
  .controls-grid {
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }

  .controls-grid {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 1.25rem;
  }

  .controls-grid label {
    flex-direction: row;
    align-items: center;
    gap: 0.4rem;
    white-space: nowrap;
  }

  .controls-grid select {
    width: auto;
  }

  #chat {
    padding: 1rem 1.5rem;
  }

  .msg,
  .msg-debug {
    max-width: 70%;
  }

  #chat-form {
    padding: 1rem 1.5rem;
  }

  .gate-box {
    padding: 2rem;
  }
}
