:root {
  /* Backgrounds */
  --bg-primary: #0a0a0a;
  --bg-ascii: #1a1a1a;
  --bg-overlay: rgba(10, 10, 10, 0.97);

  /* Text */
  --text-primary: #d4d4d4;
  --text-bright: #e8e8e8;
  --text-light: #bbb;
  --text-medium: #888;
  --text-muted: #555;
  --text-dim: #444;
  --text-joke: #e0e0e0;

  /* Accents */
  --accent-green: #4ade80;
  --accent-green-hover: #22c55e;
  --accent-orange: #e8793a;
  --accent-blue: #93c5fd;
  --accent-brown: #8b5e3c;

  /* Borders */
  --border-subtle: #222;
  --border-medium: #333;

  /* Status */
  --error: #f87171;

  /* Fonts */
  --font-mono: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  --font-serif: "Cormorant Garamond", Georgia, serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: var(--bg-primary);
  font-family: var(--font-mono);
  font-size: 14px;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

/* --- ASCII wave background --- */
#ascii-bg {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 0;
  overflow: hidden;
  color: var(--bg-ascii);
  font-size: 14px;
  line-height: 16px;
  white-space: pre;
  pointer-events: none;
  user-select: none;
}

/* --- Chat container --- */
#chat {
  position: fixed;
  z-index: 1;
  width: 50%;
  height: 70vh;
  height: 70dvh;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--bg-primary);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  color: var(--text-primary);
  overflow: hidden;
}

#header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 13px;
  flex-shrink: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
#header .header-title { color: var(--accent-green); }
#header .clock { color: var(--text-muted); }
#header .user-info {
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
}
#header .user-info:hover { color: var(--text-bright); }
#header .logo {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-family: var(--font-serif);
  font-size: 22px;
  font-weight: 500;
  color: var(--text-bright);
  letter-spacing: -0.5px;
}
#header .logo .dot { color: var(--accent-orange); }

/* --- Login overlay --- */
#login-overlay {
  position: absolute;
  inset: 0;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 10;
  gap: 16px;
}
#login-overlay.hidden { display: none; }
#login-overlay .login-title {
  font-family: var(--font-serif);
  font-size: 28px;
  font-weight: 500;
  color: var(--text-bright);
  letter-spacing: -0.5px;
}
#login-overlay .login-title .dot { color: var(--accent-orange); }
#login-overlay .login-subtitle {
  color: var(--text-muted);
  font-size: 13px;
}
#login-form {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
#key-input {
  background: transparent;
  border: 1px solid var(--border-medium);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  padding: 8px 12px;
  outline: none;
  width: 240px;
}
#key-input:focus { border-color: var(--accent-green); }
#key-input::placeholder { color: var(--text-dim); }
#login-btn {
  background: var(--accent-green);
  color: var(--bg-primary);
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
#login-btn:hover { background: var(--accent-green-hover); }
#login-btn:disabled { opacity: 0.5; cursor: default; }
#login-error {
  color: var(--error);
  font-size: 12px;
  min-height: 16px;
}

#messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 90%;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.user {
  align-self: flex-end;
  color: var(--accent-blue);
}
.msg.user::before {
  content: "> ";
  color: var(--text-muted);
}
.msg.assistant {
  align-self: flex-start;
  color: var(--text-primary);
}
.msg.assistant strong { color: var(--text-bright); }
.msg.assistant em { color: var(--text-light); }
.msg.system {
  align-self: center;
  color: var(--text-muted);
  font-size: 12px;
}
.msg.error {
  color: var(--error);
  align-self: center;
  font-size: 12px;
}
.msg.joke {
  align-self: flex-start;
  color: var(--text-joke);
  font-style: italic;
  font-size: 14px;
  line-height: 1.6;
}
.msg.tool-status {
  color: var(--accent-green);
  align-self: flex-start;
  font-size: 12px;
  opacity: 0.85;
}

.cursor {
  display: inline-block;
  width: 7px;
  height: 14px;
  background: var(--accent-green);
  animation: blink 0.8s step-end infinite;
  vertical-align: text-bottom;
  margin-left: 2px;
}
@keyframes blink { 50% { opacity: 0; } }

/* --- Tip --- */
#tip-link {
  text-align: center;
  padding: 4px 0 10px;
  flex-shrink: 0;
}
#tip-link a {
  color: var(--accent-brown);
  font-size: 11px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: color 0.2s;
}
#tip-link a:hover { color: var(--accent-orange); }

#tip-overlay {
  position: absolute;
  inset: 0;
  background: var(--bg-overlay);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 10;
  gap: 16px;
  overflow-y: auto;
  padding: 24px;
}
#tip-overlay.hidden { display: none; }
#tip-overlay .tip-title {
  font-size: 15px;
  color: var(--text-medium);
}
.tip-amounts {
  display: flex;
  gap: 8px;
}
.tip-amount-btn {
  background: transparent;
  border: 1px solid var(--border-medium);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  padding: 8px 20px;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s;
}
.tip-amount-btn:hover { border-color: var(--accent-green); color: var(--accent-green); }
.tip-amount-btn.selected {
  border-color: var(--accent-green);
  background: var(--accent-green);
  color: var(--bg-primary);
  font-weight: 600;
}
#tip-checkout-container {
  width: 100%;
  max-width: 400px;
}
#tip-close {
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
  background: none;
  border: none;
  font-family: inherit;
}
#tip-close:hover { color: var(--text-medium); }
#tip-status {
  color: var(--accent-green);
  font-size: 13px;
  min-height: 18px;
}

/* --- Video upload --- */
#video-preview {
  padding: 4px 16px;
  border-top: 1px solid var(--border-subtle);
  display: none;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}
#video-preview.active { display: flex; }
#video-preview .vp-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#video-preview .vp-status { color: var(--accent-green); }
#video-preview .vp-remove {
  color: var(--text-dim);
  cursor: pointer;
  font-size: 11px;
  transition: color 0.2s;
}
#video-preview .vp-remove:hover { color: var(--error); }

#video-progress {
  height: 2px;
  background: var(--border-subtle);
  flex-shrink: 0;
  display: none;
}
#video-progress.active { display: block; }
#video-progress-bar {
  height: 100%;
  background: var(--accent-green);
  width: 0%;
  transition: width 0.15s ease;
}

#upload-btn {
  background: transparent;
  border: 1px solid var(--border-medium);
  border-radius: 4px;
  color: #fff;
  font-family: inherit;
  font-size: 16px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  transition: border-color 0.2s, color 0.2s;
}
#upload-btn:hover { border-color: var(--accent-green); color: var(--accent-green); }
#upload-btn:disabled { cursor: default; }
#upload-btn:disabled:hover { border-color: var(--border-medium); color: #fff; }

.upload-hint {
  text-align: right;
  padding: 2px 16px 4px;
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

#input-area {
  padding: 12px 16px;
  border-top: 1px solid var(--border-subtle);
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

#prompt-label {
  color: var(--accent-green);
  line-height: 32px;
  flex-shrink: 0;
}

#input {
  flex: 1;
  background: transparent;
  border: 1px solid var(--border-medium);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  padding: 6px 10px;
  outline: none;
  resize: none;
  height: 32px;
  line-height: 18px;
}
#input:focus { border-color: var(--accent-green); }
#input::placeholder { color: var(--text-dim); }

#messages::-webkit-scrollbar { width: 6px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: var(--border-medium); border-radius: 3px; }

/* Small laptop / large tablet (e.g. Surface Pro 912px, Zenbook 853px) */
@media (max-width: 1280px) {
  #chat {
    width: 70%;
    height: 85vh;
    height: 85dvh;
  }
}

/* Tablet (e.g. iPad Air 820px) */
@media (max-width: 1024px) {
  #chat {
    width: 90%;
    height: 90vh;
    height: 90dvh;
    transition: top 0.25s ease, height 0.25s ease, width 0.25s ease, border-radius 0.15s ease, border 0.15s ease;
  }

  #header .logo {
    position: static;
    transform: none;
  }
}

/* Mobile */
@media (max-width: 768px) {
  * { -webkit-tap-highlight-color: transparent; }

  #chat {
    height: 70dvh;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    transition: border-radius 0.15s ease, border-color 0.15s ease;
  }

  #chat.keyboard-open {
    top: 0;
    left: 0;
    transform: none;
    width: 100%;
    border-radius: 0;
    border-color: transparent;
  }

  #header {
    flex-wrap: nowrap;
    padding: 10px 12px;
    gap: 4px;
  }

  #header .logo {
    font-size: 17px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  #header .header-left {
    font-size: 11px;
    overflow: hidden;
    white-space: nowrap;
  }

  #header .clock { display: none; }

  #header .user-info {
    font-size: 12px;
    flex-shrink: 0;
  }

  .msg {
    max-width: 95%;
    line-height: 1.7;
  }

  #input {
    height: 44px;
    font-size: 16px;
  }

  #upload-btn {
    width: 44px;
    height: 44px;
    font-size: 20px;
  }

  #video-preview {
    padding: 4px 12px;
  }

  #input-area {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  #login-overlay {
    justify-content: center;
    gap: 16px;
  }

  #login-overlay .login-title {
    font-size: 28px;
  }

  #login-form {
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }

  #key-input {
    width: min(220px, 70vw);
    height: 44px;
    font-size: 16px;
  }

  #login-btn {
    width: min(220px, 70vw);
    height: 44px;
    font-size: 16px;
  }

  .tip-amount-btn {
    padding: 12px 24px;
    font-size: 16px;
  }

  #tip-overlay {
    padding: 20px 16px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
  }

  #tip-checkout-container {
    max-width: 100%;
  }

  #tip-link {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  /* Touch active states */
  #login-btn:active { background: var(--accent-green-hover); }
  #upload-btn:active:not(:disabled) { border-color: var(--accent-green); color: var(--accent-green); }
  .tip-amount-btn:active { border-color: var(--accent-green); color: var(--accent-green); }

  /* Practice mode — mobile split screen */
  #chat.practice-mode {
    height: 100dvh;
    top: 0;
    left: 0;
    transform: none;
    width: 100%;
    border-radius: 0;
    border-color: transparent;
  }

  #chat.practice-mode #practice-overlay.active {
    height: 45%;
  }

  #chat.practice-mode #messages {
    flex: 1;
  }

  #practice-capture-btn {
    height: 48px;
    font-size: 16px;
    padding: 0 24px;
  }

  #practice-end-btn {
    height: 48px;
    font-size: 14px;
    padding: 0 16px;
  }
}

/* --- Live Practice Mode --- */
#practice-overlay {
  display: none;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

#practice-overlay.active {
  display: flex;
  height: 40%;
  border-bottom: 1px solid var(--border-subtle);
}

#practice-viewfinder-wrap {
  flex: 1;
  position: relative;
  background: #000;
  overflow: hidden;
}

#practice-viewfinder {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#practice-rec {
  position: absolute;
  top: 12px;
  left: 12px;
  color: var(--error);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1px;
  display: none;
}

#practice-rec.active {
  display: block;
  animation: rec-blink 1s step-end infinite;
}

@keyframes rec-blink { 50% { opacity: 0; } }

#practice-swing-label {
  position: absolute;
  top: 12px;
  right: 12px;
  color: var(--text-bright);
  font-size: 12px;
  background: rgba(0, 0, 0, 0.6);
  padding: 2px 8px;
  border-radius: 3px;
}

#practice-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-primary);
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

#practice-status {
  flex: 1;
  color: var(--accent-green);
  font-size: 12px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

#practice-capture-btn {
  background: var(--accent-green);
  color: var(--bg-primary);
  border: none;
  border-radius: 4px;
  padding: 6px 16px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

#practice-capture-btn:hover { background: var(--accent-green-hover); }
#practice-capture-btn:disabled { opacity: 0.5; cursor: default; }

#practice-end-btn {
  background: transparent;
  border: 1px solid var(--border-medium);
  border-radius: 4px;
  color: var(--text-muted);
  padding: 6px 12px;
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
}

#practice-end-btn:hover { color: var(--text-bright); border-color: var(--text-muted); }

/* Hide input area and other controls during practice */
#chat.practice-mode #input-area,
#chat.practice-mode #tip-link,
#chat.practice-mode #video-preview,
#chat.practice-mode #video-progress,
#chat.practice-mode .upload-hint {
  display: none;
}

/* Practice mode button in input area */
#practice-btn {
  background: transparent;
  border: 1px solid var(--border-medium);
  border-radius: 4px;
  color: var(--accent-green);
  font-family: inherit;
  font-size: 11px;
  padding: 4px 8px;
  cursor: pointer;
  flex-shrink: 0;
  transition: border-color 0.2s, color 0.2s;
}

#practice-btn:hover { border-color: var(--accent-green); }
#practice-btn:disabled { opacity: 0.5; cursor: default; }
