/* ========================================
   Core Styles
   ======================================== */

:root {
  /* Container */
  --app-max-width: 460px;
  --app-max-height: 720px;

  /* Colors - primary set dynamically via JS */
  --primary: hsl(220, 70%, 32%);
  --primary-dark: hsl(220, 70%, 22%);
  --background: #ffffff;
  --surface: #f8f9fa;
  --text: #1a1a1a;
  --text-light: #666666;
  --border: #e0e0e0;
  --error: #dc3545;
  --success: #28a745;
  --info: #17a2b8;

  /* Typography */
  --font-primary: 'Young Serif', serif;
  --font-body: 'Work Sans', sans-serif;

  /* Spacing (8px base) */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;

  /* Misc */
  --radius-sm: 0.5rem;
  --radius-md: 1rem;
  --radius-lg: 2rem;
  --header-height: 3.5rem;
  --input-bar-height: 3.5rem;
  --transition-speed: 0.2s;
}

/* Accessibility mode overrides */
.accessibility-mode {
  --background: #fdf6e3;
  --surface: #f5eedc;
  --font-body: 'OpenDyslexic', 'Work Sans', sans-serif;
  font-size: 112.5%;
}

@font-face {
  font-family: 'OpenDyslexic';
  src: url('../res/fonts/OpenDyslexic-Regular.otf') format('opentype');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'OpenDyslexic';
  src: url('../res/fonts/OpenDyslexic-Bold.otf') format('opentype');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'OpenDyslexic';
  src: url('../res/fonts/OpenDyslexic-Italic.otf') format('opentype');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: 'OpenDyslexic';
  src: url('../res/fonts/OpenDyslexic-BoldItalic.otf') format('opentype');
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

/* ========================================
   Base
   ======================================== */

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

body {
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: 1.5;
  color: var(--text);
  background: var(--background);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ========================================
   Utilities
   ======================================== */

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

[hidden] {
  display: none !important;
}

.skip-link {
  position: absolute;
  top: -100%;
  left: 0;
  background: var(--primary);
  color: white;
  padding: var(--space-sm) var(--space-md);
  z-index: 1000;
  font-weight: 600;
  text-decoration: none;
  border-radius: 0 0 var(--radius-sm) 0;
}

.skip-link:focus {
  top: 0;
}

/* ========================================
   App Container
   ======================================== */

.app-container {
  position: relative;
  width: 100%;
  max-width: var(--app-max-width);
  height: 100vh;
  max-height: var(--app-max-height);
  background: var(--background);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

/* ========================================
   Header
   ======================================== */

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-md);
  height: var(--header-height);
  min-height: var(--header-height);
  background: var(--primary);
  color: white;
  z-index: 10;
}

.header__title {
  font-family: var(--font-primary);
  font-size: 1.25rem;
  font-weight: 400;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header__controls {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

/* Feature toggle */
.header__toggle {
  position: relative;
  width: 3.125rem;
  height: 1.875rem;
  background: none;
  border: none;
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  border-radius: var(--radius-lg);
  padding: 0;
  min-width: 44px;
  min-height: 44px;
}

.header__toggle-track {
  position: relative;
  width: 3.2rem;
  height: 1.9rem;
  border-radius: var(--radius-lg);
  background: rgba(255, 255, 255, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.6);
  display: block;
}

.header__toggle-knob {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 50%;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header__toggle-icon {
  position: absolute;
  top: 6px;
}

.header__toggle-icon:first-child {
  width: 18px;
  height: 18px;
  right: 4px;
}

.header__toggle-icon:nth-child(2) {
  width: 16px;
  height: 16px;
  left: 4px;
}

.header__toggle:focus-visible {
  outline: 2px solid white;
  outline-offset: 2px;
}

.header__toggle[aria-checked="true"] .header__toggle-knob {
  transform: translateX(1.25rem);
}

@media (prefers-reduced-motion: no-preference) {

  .header__toggle-knob,
  .header__toggle-track {
    transition: transform var(--transition-speed) ease, background var(--transition-speed) ease;
  }
}

.header__menu-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: var(--space-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
}

.header__menu-btn:focus-visible {
  outline: 2px solid white;
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ========================================
   Main Content
   ======================================== */

.main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  background: var(--background);
}

.main__close-btn {
  position: sticky;
  top: 0;
  float: right;
  z-index: 5;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 2.25rem;
  height: 2.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin: var(--space-sm);
  color: var(--text);
}

.main__close-btn:hover {
  background: var(--border);
}

.main__close-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ========================================
   Chat
   ======================================== */

.chat {
  display: flex;
  flex-direction: column;
  padding: var(--space-md);
  min-height: 100%;
}

.chat__messages {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  flex: 1;
}

/* Message bubbles */
.message {
  max-width: 85%;
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  line-height: 1.5;
  font-size: 0.9rem;
  word-wrap: break-word;
}

.message--user {
  align-self: flex-end;
  background: var(--primary);
  color: white;
  border-bottom-right-radius: var(--space-xs);
}

.message--bot {
  align-self: flex-start;
  background: var(--surface);
  color: var(--text);
  border-bottom-left-radius: var(--space-xs);
}

.message--system {
  align-self: center;
  background: transparent;
  color: var(--text-light);
  text-align: center;
  font-size: 0.85rem;
  max-width: 95%;
}

.message--system .message__icon {
  font-size: 2rem;
  margin-bottom: var(--space-xs);
  display: block;
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: var(--space-sm) var(--space-md);
  align-self: flex-start;
  background: var(--surface);
  border-radius: var(--radius-md);
  border-bottom-left-radius: var(--space-xs);
}

.typing-indicator__dot {
  width: 8px;
  height: 8px;
  background: var(--text-light);
  border-radius: 50%;
}

@media (prefers-reduced-motion: no-preference) {
  .typing-indicator__dot {
    animation: typing-bounce 1.4s infinite ease-in-out both;
  }

  .typing-indicator__dot:nth-child(1) {
    animation-delay: 0s;
  }

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

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

@keyframes typing-bounce {

  0%,
  80%,
  100% {
    transform: scale(0.6);
    opacity: 0.4;
  }

  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* Progress bar for model loading */
.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin: var(--space-sm) 0;
}

.progress-bar__fill {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  width: 0%;
}

@media (prefers-reduced-motion: no-preference) {
  .progress-bar__fill {
    transition: width 0.3s ease;
  }
}

/* ========================================
   Alerts
   ======================================== */

.alerts {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  padding: 0 var(--space-md);
}

.alert {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  color: white;
}

@media (prefers-reduced-motion: no-preference) {
  .alert {
    animation: alert-slide-in 0.2s ease;
  }
}

@keyframes alert-slide-in {
  from {
    transform: translateY(10px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.alert--info {
  background: var(--info);
}

.alert--success {
  background: var(--success);
}

.alert--error {
  background: var(--error);
}

.alert--primary {
  background: var(--primary);
}

.alert__icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
}

.alert__text {
  flex: 1;
}

.alert__dismiss {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: var(--space-xs);
  opacity: 0.8;
  display: flex;
  align-items: center;
  min-width: 44px;
  min-height: 44px;
  justify-content: center;
}

.alert__dismiss:hover {
  opacity: 1;
}

/* ========================================
   Suggestions
   ======================================== */

.suggestions {
  display: flex;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  flex-wrap: wrap;
  justify-content: flex-end;
}

.suggestion-btn {
  background: var(--primary);
  color: white;
  border: none;
  padding: var(--space-xs) var(--space-md);
  border-radius: var(--radius-lg);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  max-width: 70%;
  text-align: right;
  line-height: 1.4;
}

@media (prefers-reduced-motion: no-preference) {
  .suggestion-btn {
    transition: transform var(--transition-speed) ease, filter var(--transition-speed) ease;
  }
}

.suggestion-btn:hover {
  transform: scale(0.98);
  filter: brightness(1.1);
}

.suggestion-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ========================================
   Input Bar
   ======================================== */

.input-bar {
  display: flex;
  align-items: flex-end;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  min-height: var(--input-bar-height);
  background: var(--background);
  border-top: 1px solid var(--border);
}

.input-bar__textarea {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-sm) var(--space-md);
  font-family: var(--font-body);
  font-size: 0.9rem;
  resize: none;
  max-height: 6rem;
  line-height: 1.5;
  color: var(--text);
  background: var(--surface);
}

.input-bar__textarea:focus {
  outline: 2px solid var(--primary);
  border-color: transparent;
}

.input-bar__textarea::placeholder {
  color: var(--text-light);
}

.input-bar__btn {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 50%;
  width: 2.5rem;
  height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
}

@media (prefers-reduced-motion: no-preference) {
  .input-bar__btn {
    transition: transform var(--transition-speed) ease, filter var(--transition-speed) ease;
  }
}

.input-bar__btn:hover {
  transform: scale(0.98);
  filter: brightness(1.1);
}

.input-bar__btn:focus-visible {
  outline: 2px solid var(--primary-dark);
  outline-offset: 2px;
}

.input-bar__btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ========================================
   Buttons
   ======================================== */

.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 0.75rem 1.5rem;
  border-radius: var(--radius-lg);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  border: none;
  text-decoration: none;
  line-height: 1;
  min-height: 44px;
}

@media (prefers-reduced-motion: no-preference) {
  .btn {
    transition: transform var(--transition-speed) ease, filter var(--transition-speed) ease;
  }
}

.btn:hover {
  transform: scale(0.98);
  filter: brightness(1.1);
}

.btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.btn--primary {
  background: var(--primary);
  color: white;
}

.btn--secondary {
  background: transparent;
  border: 2px solid var(--border);
  color: var(--text);
}

.btn--secondary:hover {
  border-color: var(--primary);
  color: var(--primary);
}

/* ========================================
   Drawer
   ======================================== */

.drawer-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 20;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 80%;
  max-width: 320px;
  background: var(--background);
  z-index: 30;
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 12px rgba(0, 0, 0, 0.15);
  will-change: transform;
}

@media (prefers-reduced-motion: no-preference) {
  .drawer {
    animation: drawer-slide-in 0.25s ease;
  }

  @keyframes drawer-slide-in {
    from {
      transform: translateX(100%);
    }

    to {
      transform: translateX(0);
    }
  }
}

.drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md);
  border-bottom: 1px solid var(--border);
}

.drawer__title {
  font-family: var(--font-primary);
  font-size: 1.1rem;
  margin: 0;
}

.drawer__close-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: var(--space-xs);
  color: var(--text);
  display: flex;
  align-items: center;
  min-width: 44px;
  min-height: 44px;
  justify-content: center;
}

.drawer__close-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.drawer__content {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
}

.drawer__section {
  margin-bottom: var(--space-lg);
}

.drawer__section-title {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-light);
  margin: 0 0 var(--space-sm);
}

.drawer__model-info {
  margin-bottom: var(--space-md);
}

.drawer__model-info p {
  margin: var(--space-xs) 0;
  font-size: 0.9rem;
}

.drawer__model-name {
  font-weight: 600;
}

.drawer__model-status {
  color: var(--text-light);
}

.drawer__btn {
  width: 100%;
  justify-content: center;
}

.drawer__accessibility-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-xs);
}

.drawer__accessibility-toggle .header__toggle-switch {
  position: relative;
  width: 2.5rem;
  height: 1.375rem;
  background: var(--border);
  border: none;
  border-radius: var(--radius-lg);
  cursor: pointer;
}

.drawer__accessibility-toggle .header__toggle-switch[aria-checked="true"] {
  background: var(--primary);
}

.drawer__accessibility-toggle .header__toggle-knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 1.125rem;
  height: 1.125rem;
  background: white;
  border-radius: 50%;
}

@media (prefers-reduced-motion: no-preference) {
  .drawer__accessibility-toggle .header__toggle-knob {
    transition: transform var(--transition-speed) ease;
  }
}

.drawer__accessibility-toggle .header__toggle-switch[aria-checked="true"] .header__toggle-knob {
  transform: translateX(1.125rem);
}

.drawer__label {
  font-size: 0.9rem;
}

.drawer__description {
  font-size: 0.8rem;
  color: var(--text-light);
  margin: 0;
}



/* ========================================
   Modal
   ======================================== */

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
}

.modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--background);
  border-radius: var(--radius-md);
  width: calc(100% - 2 * var(--space-lg));
  max-height: calc(100% - 2 * var(--space-lg));
  overflow-y: auto;
  z-index: 50;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md);
  border-bottom: 1px solid var(--border);
}

.modal__title {
  font-family: var(--font-primary);
  font-size: 1.1rem;
  margin: 0;
}

.modal__close-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: var(--space-xs);
  color: var(--text);
  display: flex;
  align-items: center;
  min-width: 44px;
  min-height: 44px;
  justify-content: center;
}

.modal__close-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.modal__form {
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.modal__fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: var(--space-sm) var(--space-md);
}

.modal__legend {
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0 var(--space-xs);
}

.modal__checkbox-label {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-xs) 0;
  font-size: 0.9rem;
  cursor: pointer;
}

.modal__field {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.modal__label {
  font-size: 0.85rem;
  font-weight: 500;
}

.modal__input,
.modal__textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: var(--space-sm) var(--space-md);
  font-family: var(--font-body);
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface);
}

.modal__input:focus,
.modal__textarea:focus {
  outline: 2px solid var(--primary);
  border-color: transparent;
}

.modal__submit {
  align-self: stretch;
  justify-content: center;
}