:root {
  color-scheme: light;
  --bg: linear-gradient(180deg, #f4f1ea 0%, #ffffff 36%, #f3f0ea 100%);
  --panel: rgba(255, 255, 255, 0.9);
  --panel-border: rgba(21, 21, 21, 0.08);
  --text: #141414;
  --muted: #666666;
  --shadow: 0 18px 60px rgba(0, 0, 0, 0.10);
  --accent: #0cc36c;
  --accent-soft: #e7fbef;
  --user-bubble: #f2ede6;
  --button: #171717;
  --button-text: #ffffff;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  min-height: 100%;
  font-family: "Segoe UI", "Noto Sans Thai", sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  min-height: 100svh;
}

body.modal-open {
  overflow: hidden;
}

/* ── ChatGPT-style grid screen ── */
.app-shell {
  min-height: 100svh;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 16px;
}

body[data-page="chat"] .app-shell {
  padding: 0;
  height: 100svh;
  min-height: 100svh;
  overflow: hidden;
}

:root {
  --keyboard-height: 0px;
  --keyboard-safe-area-bottom: max(var(--keyboard-height), env(safe-area-inset-bottom, 0px));
}

.screen {
  width: 100%;
  max-width: 460px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  display: grid;
  grid-template:
    "leading"  max-content
    "content"  1fr
    "trailing" max-content
    "keyboard" / minmax(0, 1fr);
  height: calc(100svh - 32px);
  max-height: calc(100svh - 32px);
  overflow: hidden auto;
}

body[data-page="chat"] .screen {
  max-width: none;
  border-radius: 0;
  border: none;
  box-shadow: none;
  height: 100svh;
  max-height: 100svh;
}

/* leading — sticky topbar */
[slot="leading"] {
  grid-area: leading;
  position: sticky;
  top: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  align-items: center;
  gap: 8px;
  padding: max(18px, env(safe-area-inset-top, 18px)) 18px 10px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

/* content — scrollable messages */
[slot="content"] {
  grid-area: content;
}

/* trailing — sticky composer */
[slot="trailing"] {
  grid-area: trailing;
  position: sticky;
  bottom: var(--keyboard-safe-area-bottom);
  z-index: 20;
}

/* keyboard — spacer grows with keyboard height */
[slot="keyboard"] {
  grid-area: keyboard;
  position: sticky;
  bottom: 0;
  height: var(--keyboard-safe-area-bottom);
  background: var(--panel);
}

/* ── sub-pages (settings, system-prompt) ── */
.page-card {
  width: 100%;
  max-width: 460px;
  min-height: calc(100svh - 32px);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.topbar,
.subpage-header {
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  align-items: center;
  gap: 8px;
  padding: 18px 18px 10px;
}

.topbar__title,
.subpage-title {
  margin: 0;
  text-align: center;
  font-size: 1.48rem;
  font-weight: 700;
}

.brand-title {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.64rem;
}

.brand-title__logo {
  width: 2.35rem;
  height: 2.35rem;
  object-fit: contain;
  display: block;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.10));
  transform: translateY(-0.04em);
}

.icon-button,
.back-link {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-size: 1.2rem;
}

.menu-wrap {
  position: relative;
}

.menu-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 180px;
  padding: 8px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid var(--panel-border);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.14);
  z-index: 5;
}

.menu-panel.hidden {
  display: none;
}

.menu-panel__item {
  display: block;
  padding: 12px 14px;
  border-radius: 12px;
  color: var(--text);
  text-decoration: none;
}

.menu-panel__item:hover {
  background: #f4f4f4;
}

.menu-panel__item--danger {
  width: 100%;
  border: none;
  background: transparent;
  text-align: left;
  font: inherit;
  cursor: pointer;
  color: #c0392b;
}

.menu-panel__item--danger:hover {
  background: #fff0ee;
}

.chat-list {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.chat-list::-webkit-scrollbar {
  display: none;
}

.empty-state {
  margin: auto 8px;
  padding: 24px;
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.75);
  border: 1px dashed rgba(0, 0, 0, 0.12);
  color: var(--muted);
  text-align: center;
  line-height: 1.6;
}

.message-row {
  display: flex;
  align-items: flex-end;
}

.message-row--assistant {
  justify-content: flex-start;
}

.message-row--user {
  justify-content: flex-end;
}

.bubble {
  display: inline-block;
  width: fit-content;
  max-width: 80%;
  border-radius: 14px;
  padding: 14px 12px 9px;
  line-height: 1.45;
  font-size: 0.96rem;
  white-space: normal;
  word-break: break-word;
  border: 1px solid rgba(0, 0, 0, 0.04);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.035);
}

.bubble__text {
  display: block;
  white-space: pre-wrap;
}

.bubble--assistant {
  background: linear-gradient(180deg, var(--accent-soft) 0%, #f0fbf6 100%);
  border-top-left-radius: 7px;
}

.bubble--user {
  background: var(--user-bubble);
  border-top-right-radius: 7px;
}

.message-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  flex: 0 0 auto;
  background: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.08);
  cursor: zoom-in;
}

.avatar-modal {
  position: fixed;
  inset: 0;
  z-index: 3000;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}

.avatar-modal.hidden {
  display: none;
}

.avatar-modal__image-btn {
  border: none;
  background: transparent;
  padding: 0;
  cursor: zoom-out;
}

.avatar-modal__image {
  display: block;
  max-width: min(86vw, 560px);
  max-height: 86vh;
  width: auto;
  height: auto;
  border-radius: 12px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.assistant-stack {
  position: relative;
  display: inline-block;
  max-width: calc(80% + 20px);
}

.message-avatar--overlay {
  position: absolute;
  left: -14px;
  top: -10px;
  z-index: 2;
}

.assistant-stack .bubble {
  position: relative;
  z-index: 1;
  max-width: 100%;
  margin-left: 6px;
  margin-top: 8px;
}

.composer {
  padding: 10px 14px 10px;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 0.88) 16%,
    rgba(255, 255, 255, 0.98) 100%
  );
}

.composer__option {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin: 0 2px 5px;
  font-size: 0.72rem;
  line-height: 1;
  color: #7d7d7d;
  user-select: none;
}

.composer__option-checkbox {
  width: 12px;
  height: 12px;
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid #c9c9c9;
  border-radius: 3px;
  background: #ffffff;
  display: inline-grid;
  place-items: center;
  position: relative;
  cursor: pointer;
  transition: border-color 0.16s ease;
}

.composer__option-checkbox::after {
  content: "";
  width: 6px;
  height: 3px;
  border-left: 1.6px solid #8a8a8a;
  border-bottom: 1.6px solid #8a8a8a;
  transform: rotate(-45deg) translateY(-1px);
  opacity: 0;
}

.composer__option-checkbox:checked {
  background: #ffffff;
  border-color: #bbbbbb;
}

.composer__option-checkbox:checked::after {
  opacity: 1;
}

.composer__option-checkbox:focus-visible {
  outline: 2px solid rgba(0, 0, 0, 0.12);
  outline-offset: 2px;
}

.composer__bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: stretch;
}

.composer__input,
.field__input,
.field__textarea {
  width: 100%;
  border: 1px solid rgba(0, 0, 0, 0.11);
  border-radius: 18px;
  padding: 14px 16px;
  font: inherit;
  background: rgba(255, 255, 255, 0.95);
  color: var(--text);
}

.composer__input {
  min-height: 48px;
  max-height: 220px;
  resize: none;
  padding-top: 13px;
  padding-bottom: 13px;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.03);
  overflow-y: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.composer__input::-webkit-scrollbar {
  display: none;
}

.send-button,
.clear-button,
.primary-button,
.ghost-button {
  border: none;
  border-radius: 16px;
  padding: 12px 14px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.send-button,
.clear-button {
  min-width: 62px;
  font-size: 0.82rem;
  align-self: end;
  padding-inline: 12px;
}

.send-button,
.primary-button {
  background: var(--button);
  color: var(--button-text);
}

.clear-button,
.ghost-button {
  background: #ece7e0;
  color: var(--text);
}

.send-button:disabled,
.clear-button:disabled {
  opacity: 0.6;
  cursor: wait;
}

.page-card {
  padding: 10px 14px 22px;
}

.settings-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 8px 4px 0;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field__label {
  font-weight: 700;
}

.field__hint {
  color: var(--muted);
  font-size: 0.92rem;
}

.field__textarea {
  min-height: 220px;
  resize: vertical;
}

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 6px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(23, 23, 23, 0.92);
  color: #fff;
  font-size: 0.92rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.16);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.toast--visible {
  opacity: 1;
}

@media (max-width: 520px) {
  .app-shell {
    padding: 0;
  }

  .screen,
  .page-card {
    max-width: none;
    border-radius: 0;
    border: none;
    box-shadow: none;
  }

  .screen {
    height: 100svh;
    max-height: 100svh;
  }

  .bubble,
  .assistant-stack {
    max-width: 80%;
  }
}