:root {
  --bg: var(--tg-theme-secondary-bg-color, #f3f5f7);
  --surface: var(--tg-theme-bg-color, #ffffff);
  --surface-2: color-mix(in srgb, var(--surface) 88%, var(--accent) 12%);
  --text: var(--tg-theme-text-color, #172126);
  --muted: var(--tg-theme-hint-color, #748087);
  --accent: var(--tg-theme-button-color, #247a66);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #247a66);
  --danger: var(--tg-theme-destructive-text-color, #d64b4b);
  --line: color-mix(in srgb, var(--text) 10%, transparent);
  --shadow: 0 8px 28px rgba(19, 43, 38, .08);
  --radius: 22px;
  font-family: Inter, "Segoe UI", Roboto, system-ui, sans-serif;
}

:root[data-theme="dark"] {
  --bg: #111715;
  --surface: #18201e;
  --surface-2: #1c2925;
  --text: #edf4f1;
  --muted: #9aa9a4;
  --accent: #55b99d;
  --accent-text: #092019;
  --line: rgba(255, 255, 255, .09);
  --shadow: 0 8px 28px rgba(0, 0, 0, .22);
}

* { box-sizing: border-box; }
html { background: var(--bg); min-height: 100%; }
body {
  margin: 0;
  min-width: 280px;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--text);
  background:
    radial-gradient(circle at 90% -5%, color-mix(in srgb, var(--accent) 14%, transparent), transparent 38%),
    var(--bg);
  -webkit-tap-highlight-color: transparent;
}
button, textarea, input, select { font: inherit; }
button { color: inherit; }
button:focus-visible, textarea:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 35%, transparent);
  outline-offset: 2px;
}
.app-shell { width: min(100%, 720px); min-height: 100dvh; margin: 0 auto; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 72px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  backdrop-filter: blur(18px);
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; flex: 1; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 14px;
  color: var(--accent-text);
  background: var(--accent);
  box-shadow: var(--shadow);
}
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 1px; font-size: 17px; letter-spacing: -.01em; }
.brand p { margin: 0; color: var(--muted); font-size: 12px; }
h2 { margin-bottom: 10px; font-size: clamp(24px, 7vw, 32px); line-height: 1.08; letter-spacing: -.035em; }
h3 { margin-bottom: 7px; font-size: 17px; line-height: 1.25; }
.main-content { padding: 8px 16px calc(102px + env(safe-area-inset-bottom)); }
.page { display: none; animation: page-in .24s ease both; }
.page.is-active { display: block; }
.page-stack { display: grid; gap: 14px; }
@keyframes page-in { from { opacity: 0; transform: translateY(5px); } }

.hero {
  overflow: hidden;
  position: relative;
  padding: 24px;
  border-radius: 28px;
  color: #f6fffb;
  background: linear-gradient(145deg, #175f50, #288f75);
  box-shadow: 0 15px 35px rgba(24, 105, 87, .22);
}
.hero::after {
  content: "";
  position: absolute;
  width: 180px;
  height: 180px;
  right: -65px;
  top: -75px;
  border: 34px solid rgba(255, 255, 255, .08);
  border-radius: 50%;
}
.hero p { max-width: 440px; margin-bottom: 0; color: rgba(255, 255, 255, .78); line-height: 1.55; }
.hero h2 { position: relative; z-index: 1; }
.eyebrow {
  display: inline-block;
  margin-bottom: 9px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 750;
  letter-spacing: .09em;
  text-transform: uppercase;
}
.hero .eyebrow { color: #b7eadc; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 9px; margin-top: 20px; position: relative; z-index: 1; }
.card, .composer, .result-card, .settings-card, .stat-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card, .result-card, .settings-card { padding: 18px; }
.section-heading { display: flex; align-items: end; justify-content: space-between; gap: 12px; margin: 8px 2px 0; }
.section-heading h2 { margin-bottom: 0; }
.section-heading .eyebrow { margin-bottom: 4px; }
.text-button {
  border: 0;
  padding: 8px;
  color: var(--link);
  background: transparent;
  font-weight: 700;
}
.quick-grid, .stats-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.quick-card {
  min-height: 100px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 18px;
  text-align: left;
  background: var(--surface);
}
.quick-card strong, .quick-card span { display: block; }
.quick-card span { margin-top: 7px; color: var(--muted); font-size: 13px; line-height: 1.35; }
.quick-icon { margin-bottom: 8px; color: var(--accent); font-size: 22px; }
.stat-card { padding: 16px; }
.stat-card strong { display: block; font-size: 25px; }
.stat-card span { color: var(--muted); font-size: 12px; }

.composer { padding: 12px; }
textarea, input, select {
  width: 100%;
  border: 1px solid var(--line);
  color: var(--text);
  background: var(--surface);
}
textarea {
  resize: vertical;
  min-height: 128px;
  max-height: 50vh;
  padding: 14px;
  border: 0;
  border-radius: 16px;
  line-height: 1.5;
}
textarea::placeholder, input::placeholder { color: var(--muted); }
.composer-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 4px 2px; }
.primary-button, .secondary-button, .danger-button, .ghost-button {
  min-height: 46px;
  border-radius: 15px;
  border: 0;
  padding: 11px 18px;
  font-weight: 750;
  cursor: pointer;
}
.primary-button { color: var(--accent-text); background: var(--accent); }
.secondary-button { border: 1px solid var(--line); background: var(--surface); }
.ghost-button { color: var(--accent); background: var(--surface-2); }
.danger-button { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.primary-button:disabled, .secondary-button:disabled { opacity: .55; cursor: default; }
.button-spinner { display: none; width: 17px; height: 17px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .65s linear infinite; }
.is-loading .button-label { display: none; }
.is-loading .button-spinner { display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.muted { color: var(--muted); font-size: 13px; }
.status-box { padding: 14px 16px; border-radius: 16px; color: var(--muted); background: var(--surface-2); }
.status-box.error { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, var(--surface)); }
.result-list { display: grid; gap: 11px; }
.result-card { overflow-wrap: anywhere; }
.result-card .meta { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 9px; color: var(--muted); font-size: 12px; }
.result-card .prompt { font-weight: 750; line-height: 1.4; }
.result-card .answer {
  display: -webkit-box;
  overflow: hidden;
  margin-top: 10px;
  color: var(--muted);
  line-height: 1.55;
  white-space: pre-wrap;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
}
.result-card .answer.is-expanded { display: block; }
.card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 15px; }
.small-button { min-height: 40px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 13px; color: var(--text); background: transparent; font-size: 13px; font-weight: 700; }
.small-button.accent { color: var(--accent); background: var(--surface-2); }
.small-button.danger { color: var(--danger); }
.search-field {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
}
.search-field input { height: 48px; border: 0; background: transparent; }
.empty-state { padding: 32px 20px; border: 1px dashed var(--line); border-radius: var(--radius); text-align: center; color: var(--muted); }
.empty-icon { display: block; margin-bottom: 8px; color: var(--accent); font-size: 30px; }
.profile-head { display: flex; align-items: center; gap: 15px; }
.avatar { display: grid; place-items: center; width: 62px; height: 62px; border-radius: 21px; color: var(--accent-text); background: var(--accent); font-size: 24px; font-weight: 800; object-fit: cover; }
.profile-head h2 { margin-bottom: 4px; font-size: 24px; }
.profile-head p { margin: 0; color: var(--muted); }
.setting-row { display: grid; gap: 7px; margin-bottom: 14px; }
.setting-row:last-child { margin-bottom: 0; }
.setting-row label { font-size: 13px; font-weight: 700; }
.setting-row select, .setting-row input { height: 48px; padding: 0 13px; border-radius: 14px; }
.info-row { display: flex; justify-content: space-between; gap: 14px; padding: 13px 0; border-bottom: 1px solid var(--line); }
.info-row:last-child { border-bottom: 0; }
.info-row span:first-child { color: var(--muted); }
.detail-answer { line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere; }

.icon-button {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--surface);
}
.bottom-nav {
  position: fixed;
  z-index: 30;
  bottom: 0;
  left: 50%;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  width: min(100%, 720px);
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 91%, transparent);
  backdrop-filter: blur(20px);
}
.nav-item { min-height: 56px; border: 0; border-radius: 15px; color: var(--muted); background: transparent; font-size: 10px; font-weight: 700; }
.nav-item span { display: block; }
.nav-icon { margin-bottom: 3px; font-size: 21px; line-height: 1; }
.nav-item.is-active { color: var(--accent); background: var(--surface-2); }
.nav-ai { font-size: 24px; }
.toast-region { position: fixed; z-index: 100; left: 50%; bottom: calc(85px + env(safe-area-inset-bottom)); width: min(calc(100% - 32px), 500px); transform: translateX(-50%); pointer-events: none; }
.toast { margin-top: 8px; padding: 14px 16px; border-radius: 15px; color: var(--text); background: var(--surface); box-shadow: 0 14px 40px rgba(0,0,0,.2); animation: toast-in .25s ease both; }
.toast.error { color: #fff; background: #b84444; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }
.skeleton { position: relative; overflow: hidden; border-radius: var(--radius); background: var(--surface); }
.skeleton::after { content: ""; position: absolute; inset: 0; background: linear-gradient(100deg, transparent 20%, color-mix(in srgb, var(--text) 8%, transparent) 45%, transparent 70%); transform: translateX(-100%); animation: shimmer 1.3s infinite; }
.hero-skeleton { height: 210px; }
.card-skeleton { height: 140px; }
.profile-skeleton { height: 110px; }
@keyframes shimmer { to { transform: translateX(100%); } }
.is-hidden { display: none !important; }
.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; }

@media (min-width: 600px) {
  .main-content { padding-inline: 24px; }
  .topbar { padding-inline: 24px; }
  .quick-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .stats-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; }
}
