/* Typography and theme tokens */
:root{
  --font-body: "IBM Plex Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --font-display: "Space Grotesk", "IBM Plex Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --radius-xs: 6px;
  --radius-sm: 10px;
  --radius: 14px;
  --radius-lg: 18px;
  --shadow-sm: 0 6px 16px rgba(2, 6, 23, 0.2);
  --shadow-md: 0 12px 32px rgba(2, 6, 23, 0.25);
  --shadow-lg: 0 24px 60px rgba(2, 6, 23, 0.35);
  --transition: 180ms ease;
}

html[data-theme='dark']{
  --bg: #0a0b0e;
  --bg2: #0f1116;
  --surface: rgba(14, 20, 28, 0.82);
  --surface-solid: #0f151d;
  --panel: #111925;
  --panel-2: #0f1621;
  --text: #e8eef4;
  --muted: #9db0c6;
  --border: #223044;
  --field: #0c121b;
  --link: #e2e8f0;
  --accent: #9aa3b2;
  --accent-soft: rgba(148, 163, 184, 0.2);
  --btn-start: #111827;
  --btn-end: #374151;
  --btn-shadow: rgba(17, 24, 39, 0.35);
  --danger: #f87171;
  --success: #34d399;
  --btn-text: #f9fafb;
}

html[data-theme='light']{
  --bg: #f6f7fb;
  --bg2: #eef1f6;
  --surface: rgba(255, 255, 255, 0.8);
  --surface-solid: #ffffff;
  --panel: #ffffff;
  --panel-2: #f7f9fc;
  --text: #111827;
  --muted: #4b5563;
  --border: #d7dee8;
  --field: #ffffff;
  --link: #111827;
  --accent: #1f2937;
  --accent-soft: rgba(31, 41, 55, 0.12);
  --btn-start: #111827;
  --btn-end: #000000;
  --btn-shadow: rgba(17, 24, 39, 0.25);
  --danger: #ef4444;
  --success: #10b981;
  --btn-text: #ffffff;
}

*{ box-sizing: border-box; }
html, body{ height: 100%; }
body{
  margin: 0;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.6;
  position: relative;
  min-height: 100vh;
  background-image:
    radial-gradient(1000px circle at 10% -10%, rgba(148, 163, 184, 0.16), transparent 55%),
    radial-gradient(900px circle at 90% 10%, rgba(71, 85, 105, 0.14), transparent 60%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg2) 100%);
}

body::before{
  content: "";
  position: fixed;
  inset: 0;
  background: repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.02), rgba(255, 255, 255, 0.02) 2px, transparent 2px, transparent 16px);
  pointer-events: none;
  opacity: 0.15;
}

body::after{
  content: "";
  position: fixed;
  inset: 0;
  background: radial-gradient(420px circle at 50% 18%, rgba(148, 163, 184, 0.12), transparent 65%);
  pointer-events: none;
  opacity: 0.2;
}

a{
  color: var(--link);
  text-decoration: none;
  transition: color var(--transition), opacity var(--transition);
}
a:hover{ opacity: 0.85; }

img{ max-width: 100%; }

h1, h2, h3, h4{
  font-family: var(--font-display);
  margin: 0 0 12px;
  letter-spacing: 0.2px;
}
p{ margin: 0 0 12px; }

.topbar{
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow-sm);
}
.brand{
  font-weight: 700;
  letter-spacing: 0.4px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-display);
}
.nav-actions{ display: flex; gap: 8px; align-items: center; }

.container{
  max-width: 1080px;
  margin: 28px auto;
  padding: 0 20px 32px;
}

.flash-wrap{ margin-bottom: 16px; }
.flash{
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}
.flash.success{ border-color: var(--success); }
.flash.error{ border-color: var(--danger); }

.card{
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
  margin-bottom: 18px;
  box-shadow: var(--shadow-md);
  position: relative;
  overflow: hidden;
}
.card::after{
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(420px circle at 100% 0%, rgba(148, 163, 184, 0.12), transparent 55%);
  pointer-events: none;
}
.center{ max-width: 520px; margin: 10vh auto; }

label{
  display: block;
  margin: 12px 0 6px;
  font-weight: 600;
  color: var(--text);
}

input, textarea, select{
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font-size: 16px;
  transition: border-color var(--transition), box-shadow var(--transition), background var(--transition);
}
input:focus, textarea:focus, select:focus{
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

textarea{ resize: vertical; min-height: 90px; }

button, .btn{
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: linear-gradient(135deg, var(--btn-start), var(--btn-end));
  color: var(--btn-text);
  font-weight: 600;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition), opacity var(--transition);
  box-shadow: 0 10px 24px var(--btn-shadow);
  text-transform: none;
  width: auto;
}

button:hover, .btn:hover{ transform: translateY(-1px); opacity: 0.95; }
button:disabled, .btn:disabled{ opacity: 0.6; cursor: not-allowed; transform: none; box-shadow: none; }

.btn-ghost{
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: none;
}
.btn-ghost:hover{ background: rgba(255, 255, 255, 0.05); }

.btn-danger{
  background: linear-gradient(135deg, #f97316, #ef4444);
  color: #fff;
  box-shadow: 0 10px 24px rgba(239, 68, 68, 0.25);
}

.small{ padding: 8px 12px; font-size: 0.85rem; border-radius: var(--radius-xs); }
button.small, .btn.small{ width: auto; }

.checkbox{ display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.checkbox input{ width: auto; }

.grid{
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}
@media (max-width: 900px){ .grid{ grid-template-columns: 1fr; } }

.list{ list-style: none; padding: 0; margin: 0; }
.list li{
  padding: 10px 0;
  border-bottom: 1px dashed var(--border);
}
.chat-link{ display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.badge{
  font-size: 12px;
  background: rgba(148, 163, 184, 0.14);
  border: 1px solid rgba(148, 163, 184, 0.35);
  padding: 2px 8px;
  border-radius: 999px;
  color: var(--accent);
  font-weight: 600;
}

.profile{ display: flex; gap: 18px; align-items: flex-start; }
.avatar{
  width: 64px;
  height: 64px;
  border-radius: 16px;
  border: 2px solid var(--border);
  object-fit: cover;
  box-shadow: var(--shadow-sm);
}
.profile .profile-fields{ flex: 1; }

.muted{ color: var(--muted); }

/* Admin table */
.data-table{
  width: 100%;
  border-collapse: collapse;
}
.data-table th, .data-table td{
  padding: 10px 12px;
  border-bottom: 1px dashed var(--border);
  text-align: left;
}
.data-table thead th{
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.data-table tbody tr:hover{
  background: rgba(255, 255, 255, 0.03);
}
.pill{
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--muted);
}
.actions{
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}
.actions form{ display: inline; }
.inline-input{ max-width: 220px; }

/* Chat */
.chatgpt{
  max-width: 920px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  height: 100svh;
  height: 100dvh;
}
.chat-title{
  padding: 16px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 12px;
  display: grid;
  gap: 4px;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: var(--shadow-sm);
}
.chat-title .title-main{ font-size: 1.05rem; font-weight: 700; }
.chat-title .title-sub{ font-size: 0.85rem; }
.chat-title .actions{
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 8px;
}

.messages{
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  padding-bottom: var(--messages-pad-bottom, 18px);
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: var(--shadow-sm);
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.msg{
  display: flex;
  gap: 12px;
  align-items: flex-start;
  max-width: 84%;
}
.msg.user{ margin-left: auto; flex-direction: row; }
.msg.assistant{ margin-right: auto; flex-direction: row-reverse; }

.msg .avatar{
  flex: 0 0 32px;
  height: 32px;
  width: 32px;
  border-radius: 10px;
  background: rgba(148, 163, 184, 0.18);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  border: 1px solid rgba(148, 163, 184, 0.35);
}

.msg .bubble{
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel);
  box-shadow: var(--shadow-sm);
}
.msg.user .bubble{
  background: linear-gradient(180deg, rgba(148, 163, 184, 0.16), rgba(11, 17, 22, 0.92));
  border-color: rgba(148, 163, 184, 0.35);
}
html[data-theme='light'] .msg.user .bubble{
  background: linear-gradient(180deg, rgba(148, 163, 184, 0.2), #ffffff);
}

.msg .meta{ font-size: 0.8rem; color: var(--muted); margin-bottom: 8px; }
.msg .text{ white-space: pre-wrap; word-break: break-word; line-height: 1.6; font-size: 0.98rem; }

.msg.incoming .bubble{ opacity: 0; transform: translateY(8px) scale(0.98); }
.msg.enter .bubble{ opacity: 1; transform: none; transition: transform 260ms ease, opacity 260ms ease; }
.msg[data-temp='1'] .bubble{
  opacity: 0.85;
  background-image: linear-gradient(90deg, rgba(255, 255, 255, 0.06), transparent 30%, rgba(255, 255, 255, 0.06) 60%, transparent);
  background-size: 200% 100%;
  animation: sendShimmer 1.2s ease-in-out infinite;
}
@keyframes sendShimmer{
  0%{ background-position: 200% 0; }
  100%{ background-position: -200% 0; }
}

.msg .bubble pre, .msg .bubble code{
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
}
.msg .bubble pre{
  background: rgba(0, 0, 0, 0.2);
  padding: 10px;
  border-radius: 8px;
  overflow: auto;
}

.chat-actions-wrap{
  display: flex;
  justify-content: flex-end;
  margin: 8px 0;
}

.composer{
  margin-top: 12px;
  padding: 12px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  display: flex;
  gap: 12px;
  align-items: flex-end;
  box-shadow: var(--shadow-sm);
  position: sticky;
  bottom: 0;
  z-index: 10;
}
.composer textarea{
  flex: 1;
  min-height: 44px;
  max-height: 160px;
  background: var(--field);
}
.composer button{ min-width: 92px; }

body.chat-fullscreen .topbar{ display: none; }
body.chat-theater .container{ max-width: 100%; padding-left: 10px; padding-right: 10px; }
body.chat-theater .chatgpt{ max-width: 100%; }

.dots{ display: inline-flex; gap: 6px; align-items: center; }
.dots span{
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  display: inline-block;
  opacity: 0.6;
  animation: dotPulse 1.1s infinite ease-in-out;
}
.dots span:nth-child(2){ animation-delay: 0.15s; }
.dots span:nth-child(3){ animation-delay: 0.3s; }
@keyframes dotPulse{
  0%, 80%, 100%{ transform: translateY(0); opacity: 0.5; }
  40%{ transform: translateY(-4px); opacity: 1; }
}

/* Auth pages */
.auth-header{ margin-bottom: 16px; }
.auth-form button{ width: 100%; margin-top: 14px; }
.password-wrap{ display: flex; gap: 12px; align-items: center; margin-bottom: 10px; }
.password-wrap input{ flex: 1; }

.password-requirements{ margin: 10px 0; font-size: 0.9em; }
.password-requirements ul{ margin: 6px 0; padding-left: 18px; }
.password-requirements li{ margin: 4px 0; color: var(--muted); }
.password-requirements li.valid{ color: var(--success); }
.password-match-error{ color: var(--danger); font-size: 0.9em; margin-top: 6px; }

/* Utility */
hr{ border: none; border-top: 1px dashed var(--border); margin: 14px 0; }

@media (max-width: 768px){
  body{ font-size: 15px; }
  .container{ margin: 20px auto; padding: 0 14px 24px; }
  .topbar{ padding: 12px 14px; }
  .brand{ font-size: 0.95rem; }
  .card{ padding: 18px; }
  .chatgpt{ height: auto; min-height: 100vh; }
  .chat-title{ padding: 12px; }
  .chat-title .actions{ position: static; transform: none; justify-content: center; margin-top: 6px; }
  .messages{ padding: 12px; }
  .msg{ max-width: 92%; gap: 10px; }
  .composer{ padding: 10px; flex-direction: column; align-items: stretch; }
  .composer button{ width: 100%; }
  .profile{ flex-direction: column; align-items: flex-start; }
}

@media (max-width: 480px){
  .container{ padding: 0 12px 20px; }
  .chat-title .title-main{ font-size: 0.95rem; }
  .chat-title .title-sub{ font-size: 0.8rem; }
  .msg .bubble{ padding: 12px; }
}

/* Prevent iOS zoom */
@media screen and (max-width: 768px){
  input, textarea, button{ font-size: 16px !important; }
}

/* Safe area insets */
@supports (padding-bottom: env(safe-area-inset-bottom)){
  .composer{ padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .chat-title{ padding-top: calc(12px + env(safe-area-inset-top)); }
}
