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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242736;
  --border: #2e3144;
  --text: #e4e4e7;
  --text-dim: #8b8d98;
  --accent: #6c8aff;
  --accent-dim: #4a5e99;
  --user-bubble: #2a3a5c;
  --assistant-bubble: #1e2030;
  --danger: #e5484d;
  --success: #30a46c;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.hidden {
  display: none !important;
}

/* Auth Screen */
.auth-container {
  margin: auto;
  text-align: center;
  padding: 2rem;
  width: 100%;
  max-width: 320px;
}

.logo {
  width: 72px;
  height: 72px;
  background: linear-gradient(135deg, var(--accent), #9f7aea);
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
  font-weight: 700;
  color: white;
  margin: 0 auto 1.5rem;
}

.auth-container h1 {
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.auth-container p {
  color: var(--text-dim);
  margin-bottom: 2rem;
  font-size: 0.9rem;
}

#token-input {
  width: 100%;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 1rem;
  outline: none;
  margin-bottom: 1rem;
}

#token-input:focus {
  border-color: var(--accent);
}

#connect-btn {
  width: 100%;
  padding: 0.75rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

#connect-btn:active {
  opacity: 0.8;
}

/* Chat Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  padding-top: max(0.75rem, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.logo-sm {
  width: 30px;
  height: 30px;
  background: linear-gradient(135deg, var(--accent), #9f7aea);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  color: white;
}

.title {
  font-weight: 600;
  font-size: 1.1rem;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
}

.status-dot.connected {
  background: var(--success);
}

.status-dot.thinking {
  background: var(--accent);
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* Cron Panel */
.cron-panel {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  max-height: 60vh;
  overflow-y: auto;
  flex-shrink: 0;
}

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

.cron-header h3 {
  font-size: 0.95rem;
  font-weight: 600;
}

.cron-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
}

.cron-list {
  padding: 0.5rem;
}

.cron-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  border-radius: 8px;
  margin-bottom: 0.25rem;
  font-size: 0.85rem;
}

.cron-item:hover {
  background: var(--surface2);
}

.cron-item.disabled {
  opacity: 0.4;
}

.cron-num {
  color: var(--text-dim);
  font-size: 0.75rem;
  width: 1.5rem;
  text-align: center;
  flex-shrink: 0;
}

.cron-info {
  flex: 1;
  min-width: 0;
}

.cron-name {
  font-weight: 600;
  color: var(--text);
}

.cron-schedule {
  color: var(--text-dim);
  font-size: 0.75rem;
  font-family: 'SF Mono', 'Consolas', monospace;
}

.cron-prompt-text {
  color: var(--text-dim);
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cron-actions {
  display: flex;
  gap: 0.35rem;
  flex-shrink: 0;
}

.cron-actions button {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 4px;
  padding: 0.2rem 0.4rem;
  font-size: 0.7rem;
  cursor: pointer;
}

.cron-actions button:active {
  opacity: 0.6;
}

.cron-actions button.danger {
  color: var(--danger);
  border-color: var(--danger);
}

.cron-add-btn {
  display: block;
  width: calc(100% - 1rem);
  margin: 0.5rem;
  padding: 0.5rem;
  background: var(--surface2);
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--accent);
  font-size: 0.85rem;
  cursor: pointer;
  text-align: center;
}

.cron-form {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
}

.cron-form input, .cron-form select {
  width: 100%;
  padding: 0.5rem 0.65rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.85rem;
  margin-bottom: 0.4rem;
  outline: none;
}

.cron-form input:focus {
  border-color: var(--accent);
}

.cron-form-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.cron-form-actions button {
  flex: 1;
  padding: 0.5rem;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  border: 1px solid var(--border);
}

.cron-form-actions button:first-child {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.cron-cancel {
  background: var(--surface2);
  color: var(--text);
}

#history-btn, #cron-btn, #notif-btn, #refresh-btn, #new-chat-btn {
  width: 30px;
  height: 30px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#history-btn:active, #cron-btn:active, #notif-btn:active, #refresh-btn:active, #new-chat-btn:active {
  opacity: 0.6;
}

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.message {
  max-width: 88%;
  padding: 0.75rem 1rem;
  border-radius: 14px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user {
  align-self: flex-end;
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
}

.message.assistant {
  align-self: flex-start;
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  max-width: 95%;
}

.message.error {
  align-self: center;
  background: rgba(229, 72, 77, 0.15);
  border: 1px solid var(--danger);
  color: var(--danger);
  font-size: 0.85rem;
}

.message.system {
  align-self: center;
  background: rgba(108, 138, 255, 0.1);
  border: 1px solid var(--accent-dim);
  color: var(--accent);
  font-size: 0.85rem;
  max-width: 95%;
}

/* Markdown in messages */
.message.assistant h1, .message.assistant h2, .message.assistant h3 {
  margin: 0.75rem 0 0.25rem;
  font-size: 1rem;
  font-weight: 600;
}

.message.assistant h1:first-child, .message.assistant h2:first-child, .message.assistant h3:first-child {
  margin-top: 0;
}

.message.assistant p {
  margin: 0.4rem 0;
}

.message.assistant p:first-child {
  margin-top: 0;
}

.message.assistant p:last-child {
  margin-bottom: 0;
}

.message.assistant ul, .message.assistant ol {
  margin: 0.4rem 0;
  padding-left: 1.25rem;
}

.message.assistant li {
  margin: 0.15rem 0;
}

.message.assistant code {
  background: var(--surface2);
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  font-size: 0.85em;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

.message.assistant pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  margin: 0.5rem 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.message.assistant pre code {
  background: none;
  padding: 0;
  font-size: 0.8em;
  white-space: pre;
}

.message.assistant strong {
  font-weight: 600;
  color: #fff;
}

.message.assistant a {
  color: var(--accent);
  text-decoration: none;
}

.message.assistant table {
  border-collapse: collapse;
  margin: 0.5rem 0;
  font-size: 0.85em;
  width: 100%;
}

.message.assistant th, .message.assistant td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.5rem;
  text-align: left;
}

.message.assistant th {
  background: var(--surface2);
  font-weight: 600;
}

/* Activity indicator */
.activity {
  padding: 0.5rem 1rem;
  font-size: 0.8rem;
  color: var(--accent);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.activity::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s infinite;
  flex-shrink: 0;
}

#activity-text {
  flex: 1;
}

#cancel-btn {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 6px;
  padding: 0.15rem 0.5rem;
  font-size: 0.75rem;
  cursor: pointer;
  flex-shrink: 0;
}

#cancel-btn:active {
  opacity: 0.6;
}

/* Input Area */
.input-area {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#input {
  flex: 1;
  padding: 0.65rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  line-height: 1.4;
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
}

#input:focus {
  border-color: var(--accent);
}

#send-btn {
  width: 40px;
  height: 40px;
  background: var(--accent);
  border: none;
  border-radius: 12px;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

#send-btn:disabled {
  background: var(--accent-dim);
  cursor: not-allowed;
}

#send-btn:active:not(:disabled) {
  opacity: 0.8;
}

/* Scrollbar */
.messages::-webkit-scrollbar {
  width: 4px;
}

.messages::-webkit-scrollbar-track {
  background: transparent;
}

.messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* Message wrapper for copy button */
.message {
  position: relative;
}

.msg-copy {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  width: 24px;
  height: 24px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 1;
  padding: 0;
}

.message:hover .msg-copy {
  opacity: 1;
}

.msg-copy:active {
  opacity: 0.6;
}

.msg-copy.copied {
  color: var(--success);
  opacity: 1;
}

/* Always visible on touch devices */
@media (hover: none) {
  .msg-copy {
    opacity: 0.7;
  }
}

/* History panel items */
.history-item {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  margin-bottom: 0.25rem;
  cursor: pointer;
  font-size: 0.85rem;
}

.history-item:hover {
  background: var(--surface2);
}

.history-item.active {
  border-left: 3px solid var(--accent);
  background: var(--surface2);
}

.history-date {
  font-size: 0.7rem;
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
}

.history-preview {
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-count {
  color: var(--text-dim);
  font-size: 0.7rem;
}

/* Timestamp */
.msg-time {
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-top: 0.35rem;
}
