:root {
  color-scheme: light;
  --bg: #f8f6f7;
  --surface: #ffffff;
  --text: #17202b;
  --muted: #6b7280;
  --line: #e5e0e2;
  --wine: #8a0030;
  --wine-dark: #650020;
  --danger: #b42318;
  --safe-bottom: env(safe-area-inset-bottom, 8px);
  --safe-top: env(safe-area-inset-top, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
  border: 0;
  background: none;
  padding: 0;
}

/* ── App Shell ── */
.app-shell {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
}

.login-view,
.chat-view {
  display: none;
  height: 100%;
  height: 100dvh;
}

.app-shell[data-view="login"] .login-view,
.app-shell[data-view="chat"] .chat-view {
  display: flex;
}

/* ── Login ── */
.login-view {
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at 50% 30%, rgba(138, 0, 48, 0.06), transparent 60%), var(--bg);
}

.login-panel {
  width: 100%;
  max-width: 360px;
  padding: 32px 28px;
  border-radius: 24px;
  background: var(--surface);
  box-shadow: 0 8px 40px rgba(23, 32, 43, 0.1);
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--wine);
  font-size: 0.75rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

h1 {
  margin: 0;
  font-size: 1.65rem;
  line-height: 1.15;
  letter-spacing: -0.01em;
}

.field {
  display: grid;
  gap: 8px;
  margin: 28px 0 20px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.field input {
  width: 100%;
  min-height: 52px;
  border: 1.5px solid var(--line);
  border-radius: 14px;
  padding: 0 16px;
  color: var(--text);
  background: #fff;
  outline: none;
  font-size: 1rem;
}

.field input:focus {
  border-color: rgba(138, 0, 48, 0.45);
  box-shadow: 0 0 0 4px rgba(138, 0, 48, 0.1);
}

.primary-button {
  width: 100%;
  min-height: 52px;
  border-radius: 14px;
  background: var(--wine);
  color: #fff;
  font-weight: 700;
  font-size: 1rem;
}

.primary-button:active {
  background: var(--wine-dark);
}

.primary-button:disabled {
  opacity: 0.55;
}

.form-error {
  min-height: 22px;
  margin: 14px 0 0;
  color: var(--danger);
  font-size: 0.88rem;
}

/* ── Chat View ── */
.chat-view {
  flex-direction: column;
}

/* ── Header ── */
.chat-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  padding-top: calc(10px + var(--safe-top));
  background: var(--wine);
  color: #fff;
}

.header-left {
  display: flex;
  flex-direction: column;
}

.header-left strong {
  font-size: 1.15rem;
  line-height: 1.15;
}

.header-left span {
  font-size: 0.7rem;
  opacity: 0.75;
  line-height: 1.15;
}

.logout-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff;
}

.logout-btn:active {
  background: rgba(255, 255, 255, 0.15);
}

/* ── Hero ── */
.chat-hero {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 16px 12px;
  text-align: center;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.hero-bot {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(138, 0, 48, 0.08);
  color: var(--wine);
  font-size: 1.6rem;
}

.chat-hero h2 {
  margin: 6px 0 2px;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
}

.chat-hero p {
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
}

/* ── Messages Area ── */
.messages-area {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 14px 4px;
  background: var(--bg);
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.message {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 0.92rem;
  line-height: 1.48;
  overflow-wrap: break-word;
  word-break: break-word;
}

.message.user {
  align-self: flex-end;
  background: var(--wine);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.message.assistant {
  align-self: flex-start;
  background: var(--surface);
  color: var(--text);
  border-bottom-left-radius: 6px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.message.system {
  align-self: center;
  background: #fef2f2;
  color: var(--danger);
  font-size: 0.82rem;
  text-align: center;
  max-width: 92%;
}

.message-content > :first-child {
  margin-top: 0;
}

.message-content > :last-child {
  margin-bottom: 0;
}

.message-content p {
  margin: 0 0 0.4em;
}

.message-content h3,
.message-content h4,
.message-content h5 {
  margin: 0.5em 0 0.25em;
  font-size: 0.92rem;
  line-height: 1.2;
}

.message-content ul {
  margin: 0.2em 0 0.4em;
  padding-left: 1.1rem;
}

.message-content li {
  margin: 0.1em 0;
}

.message-content code {
  border-radius: 4px;
  padding: 0.1em 0.3em;
  background: rgba(0, 0, 0, 0.06);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.88em;
}

.message.user .message-content code {
  background: rgba(255, 255, 255, 0.18);
}

.message-content pre {
  max-width: 100%;
  margin: 0.3em 0;
  overflow-x: auto;
  border-radius: 8px;
  padding: 8px;
  background: #1f2328;
  color: #f6f8fa;
}

.message-content pre code {
  padding: 0;
  background: transparent;
  color: inherit;
}

.table-wrap {
  max-width: 100%;
  margin: 0.3em 0;
  overflow-x: auto;
}

.message-content table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.message-content th,
.message-content td {
  border: 1px solid var(--line);
  padding: 5px 7px;
  text-align: left;
  vertical-align: top;
}

.message-content th {
  background: var(--bg);
  font-weight: 700;
}

/* ── Typing indicator ── */
.typing-indicator {
  display: none;
  align-items: center;
  gap: 4px;
  padding: 0 18px 4px;
}

.typing-indicator.visible {
  display: flex;
}

.typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  opacity: 0.35;
  animation: typingBounce 1.4s ease-in-out infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ── Composer ── */
.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: max(8px, var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  max-height: 120px;
  min-height: 42px;
  resize: none;
  border: 1.5px solid var(--line);
  border-radius: 21px;
  padding: 9px 16px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.35;
  outline: none;
  font-size: 0.92rem;
}

.composer textarea:focus {
  border-color: rgba(138, 0, 48, 0.4);
  box-shadow: 0 0 0 3px rgba(138, 0, 48, 0.08);
}

.composer textarea::placeholder {
  color: #9ca3af;
}

.send-button {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--wine);
  color: #fff;
  flex-shrink: 0;
}

.send-button:active {
  background: var(--wine-dark);
}

.send-button:disabled {
  opacity: 0.4;
}

/* ── Utility ── */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ── Tutorial ── */
.tutorial-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
}

.tutorial-overlay.active {
  display: flex;
}

.tutorial-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.tutorial-card {
  position: relative;
  width: min(92%, 360px);
  background: var(--surface);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.25);
  animation: tutorialIn 0.35s ease-out;
}

@keyframes tutorialIn {
  from { opacity: 0; transform: translateY(20px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.tutorial-steps {
  padding: 28px 24px 8px;
  min-height: 200px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.tutorial-step {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  animation: stepFadeIn 0.3s ease-out;
}

.tutorial-step.active {
  display: flex;
}

@keyframes stepFadeIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

.tutorial-icon {
  width: 64px;
  height: 64px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(138, 0, 48, 0.08);
  color: var(--wine);
}

.tutorial-step h3 {
  margin: 0;
  font-size: 1.1rem;
  color: var(--text);
}

.tutorial-step p {
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.45;
}

.tutorial-demo-chat {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 4px;
}

.tutorial-demo-bubble {
  max-width: 82%;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 0.85rem;
  line-height: 1.4;
  text-align: left;
  opacity: 0;
  transform: translateY(10px);
}

.tutorial-demo-bubble.user {
  align-self: flex-end;
  background: var(--wine);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.tutorial-demo-bubble.assistant {
  align-self: flex-start;
  background: #f3f3f3;
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.tutorial-demo-bubble.animate {
  animation: bubblePop 0.4s ease-out forwards;
}

.tutorial-demo-bubble:nth-child(1).animate { animation-delay: 0.2s; }
.tutorial-demo-bubble:nth-child(2).animate { animation-delay: 0.9s; }
.tutorial-demo-bubble:nth-child(3).animate { animation-delay: 1.6s; }
.tutorial-demo-bubble:nth-child(4).animate { animation-delay: 2.3s; }

@keyframes bubblePop {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.tutorial-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px 22px;
}

.tutorial-skip {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 500;
}

.tutorial-dots {
  display: flex;
  gap: 8px;
}

.tutorial-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--line);
  transition: background 0.2s;
}

.tutorial-dot.active {
  background: var(--wine);
  width: 24px;
  border-radius: 4px;
}

.tutorial-next {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--wine);
}
