:root {
  --bg-cream: #f6f1e8;
  --bg-warm: #f1e8d8;
  --bg-ink: #201814;
  --card: rgba(255, 251, 244, 0.82);
  --card-strong: rgba(255, 248, 237, 0.92);
  --line: rgba(142, 90, 56, 0.22);
  --ink: #251c16;
  --muted: #6f5d50;
  --accent: #be4a20;
  --accent-2: #8e2f1f;
  --accent-3: #cc7a3b;
  --ok: #1f7a55;
  --focus: #246bff;
  --shadow-lg: 0 30px 70px rgba(44, 26, 16, 0.16);
  --shadow-md: 0 18px 44px rgba(44, 26, 16, 0.12);
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: 'Manrope', sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, rgba(190, 74, 32, 0.24), transparent 36%),
    radial-gradient(circle at 90% 5%, rgba(204, 122, 59, 0.2), transparent 30%),
    radial-gradient(circle at 92% 88%, rgba(142, 47, 31, 0.12), transparent 28%),
    linear-gradient(135deg, var(--bg-cream) 0%, #f3eadc 45%, var(--bg-warm) 100%);
  position: relative;
  overflow-x: hidden;
}

.grain {
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.1;
  background-image: radial-gradient(circle at 1px 1px, rgba(0, 0, 0, 0.1) 1px, transparent 0);
  background-size: 4px 4px;
}

.app-shell {
  width: min(1280px, 94vw);
  margin: 30px auto;
  position: relative;
  z-index: 1;
}

body.mode-login .app-shell {
  width: min(640px, 95vw);
  margin: 0 auto;
  min-height: 100svh;
  min-height: 100dvh;
  display: grid;
  align-items: center;
  padding: 12px 0;
}

body.mode-login .login-card {
  width: min(640px, 95vw);
  max-height: calc(100svh - 24px);
  max-height: calc(100dvh - 24px);
  overflow: auto;
}

body.mode-dashboard .app-shell {
  width: min(1280px, 94vw);
  margin: 30px auto;
  min-height: 0;
  display: block;
  padding: 0;
}

.login-card,
.panel,
.kpi-card,
.topbar {
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-md);
}

.login-card {
  width: min(560px, 100%);
  padding: 36px;
  border-radius: 28px;
  animation: rise 480ms ease both;
}

.brand-logo {
  width: 92px;
  height: auto;
  margin-bottom: 8px;
}

.brand {
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 12px;
  font-weight: 800;
  color: var(--accent-2);
}

.eyebrow {
  margin: 8px 0 0;
  font-size: 11px;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--ok);
  font-weight: 800;
}

h1,
h2,
h3 {
  font-family: 'Fraunces', serif;
  margin: 10px 0;
  color: #1f1612;
}

h1 {
  font-size: clamp(36px, 5vw, 54px);
  line-height: 1.02;
}

h2 {
  font-size: clamp(30px, 4vw, 44px);
  line-height: 1;
}

h3 {
  font-size: clamp(24px, 3vw, 33px);
  line-height: 1.06;
}

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

.login-form {
  margin-top: 24px;
  display: grid;
  gap: 14px;
}

label {
  display: grid;
  gap: 8px;
  font-weight: 700;
  color: #3e3128;
}

input,
select {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 16px;
  line-height: 1.2;
  background: rgba(255, 255, 255, 0.84);
  color: #2c2018;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

input:focus,
select:focus {
  border-color: color-mix(in srgb, var(--focus) 40%, var(--line));
  box-shadow: 0 0 0 3px rgba(36, 107, 255, 0.14);
  background: #fff;
}

button {
  border: none;
  border-radius: 14px;
  padding: 12px 16px;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.2s ease;
  background: linear-gradient(105deg, var(--accent), var(--accent-3));
  color: #fff8f2;
}

button:hover {
  filter: brightness(1.05);
}

button:active {
  transform: translateY(1px);
}

button.ghost {
  background: rgba(255, 255, 255, 0.7);
  color: var(--ink);
  border: 1px solid var(--line);
}

.error {
  min-height: 18px;
  color: #b12f2f;
  font-size: 14px;
  margin-top: 4px;
}

.hidden {
  display: none;
}

.dashboard {
  display: grid;
  gap: 18px;
  animation: rise 420ms ease both;
}

.dashboard.hidden {
  display: none;
}

.topbar {
  border-radius: 24px;
  padding: 20px 22px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  background: linear-gradient(140deg, rgba(255, 251, 244, 0.95), rgba(255, 245, 230, 0.84));
  box-shadow: var(--shadow-lg);
}

.top-actions {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.inline-field {
  display: grid;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 800;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.kpi-card {
  border-radius: 20px;
  padding: 16px;
  background: linear-gradient(160deg, var(--card-strong), rgba(255, 243, 226, 0.88));
}

.kpi-label {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: #7a6556;
  font-weight: 800;
}

.kpi-value {
  margin: 8px 0 0;
  font-family: 'Fraunces', serif;
  font-size: clamp(27px, 3vw, 38px);
  line-height: 1.06;
}

.two-col {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 12px;
}

.panel {
  border-radius: 22px;
  padding: 18px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.6), rgba(255, 248, 236, 0.82)),
    linear-gradient(145deg, rgba(255, 255, 255, 0.1), rgba(190, 74, 32, 0.03));
  border: 1px solid color-mix(in srgb, var(--line) 85%, #fff 15%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.42), var(--shadow-md);
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px dashed rgba(142, 90, 56, 0.22);
}

.controls-inline {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.controls-inline button.ghost {
  background: rgba(255, 255, 255, 0.85);
}

.controls-inline input[type='number'] {
  width: 86px;
}

.rank-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.rank-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  border-bottom: 1px dashed rgba(148, 113, 84, 0.25);
  padding-bottom: 8px;
}

.rank-item span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bars {
  display: grid;
  gap: 9px;
}

.bar-row {
  display: grid;
  grid-template-columns: 96px 1fr auto;
  gap: 10px;
  align-items: center;
}

.bar-track {
  height: 12px;
  border-radius: 999px;
  background: rgba(46, 31, 22, 0.12);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-2), var(--accent-3), #2f8f72);
}

.chat-panel {
  display: grid;
  gap: 10px;
}

.chat-log {
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px;
  min-height: 210px;
  max-height: 360px;
  overflow-y: auto;
  display: grid;
  gap: 10px;
}

.msg {
  max-width: min(92%, 900px);
  padding: 11px 14px;
  border-radius: 16px;
  line-height: 1.42;
  white-space: pre-line;
  font-size: 16px;
}

.msg.user {
  justify-self: end;
  background: rgba(190, 74, 32, 0.1);
  border: 1px solid rgba(190, 74, 32, 0.34);
}

.msg.bot {
  justify-self: start;
  background: rgba(255, 252, 247, 0.94);
  border: 1px solid rgba(190, 74, 32, 0.26);
}

.msg.typing {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
}

.typing-dots i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(142, 47, 31, 0.7);
  animation: dotPulse 1.2s infinite ease-in-out;
}

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

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

.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.panel-expand {
  cursor: zoom-in;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(28, 18, 13, 0.62);
  display: grid;
  place-items: center;
  z-index: 20;
}

.modal.hidden {
  display: none;
}

.modal-inner {
  width: min(980px, 94vw);
  max-height: 86vh;
  overflow: auto;
  background: #fff8ee;
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow-lg);
  padding: 16px;
}

.modal-close {
  float: right;
}

.modal-body {
  margin-top: 10px;
}

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

@keyframes dotPulse {
  0%,
  80%,
  100% {
    transform: scale(0.7);
    opacity: 0.4;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@media (max-width: 1024px) {
  .app-shell {
    width: min(100%, 95vw);
    margin: 20px auto;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .two-col {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: stretch;
  }

  .top-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100%, 97vw);
    margin: 12px auto;
  }

  body.mode-login .app-shell {
    width: min(100%, 97vw);
    margin: 0 auto;
    min-height: 100svh;
    min-height: 100dvh;
    padding: 8px 0;
  }

  body.mode-login .login-card {
    max-height: calc(100svh - 16px);
    max-height: calc(100dvh - 16px);
  }

  .login-card,
  .topbar,
  .panel,
  .kpi-card {
    border-radius: 16px;
    padding: 14px;
  }

  .kpi-grid {
    grid-template-columns: 1fr;
  }

  .panel-head {
    align-items: stretch;
  }

  .controls-inline {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  .controls-inline > * {
    width: 100%;
    min-width: 0;
  }

  .controls-inline input[type='number'] {
    width: 100%;
  }

  .bar-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .chat-log {
    min-height: 220px;
    max-height: 48vh;
  }

  .msg {
    max-width: 100%;
    font-size: 16px;
  }

  .chat-form {
    grid-template-columns: 1fr;
  }

  .chat-form button {
    width: 100%;
  }

  .modal-inner {
    width: 100vw;
    max-height: 100vh;
    border-radius: 0;
    padding: 12px;
  }
}

@media (max-height: 780px) {
  body.mode-login .app-shell {
    align-items: start;
    padding: 8px 0;
  }

  body.mode-login .login-card {
    padding: 20px;
    border-radius: 18px;
    max-height: calc(100svh - 16px);
    max-height: calc(100dvh - 16px);
  }
}
