/* ============================================================
   Mika — Mobile-first Dark Theme
   ============================================================ */

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --bg-input: #222244;
  --bg-user-msg: #1e2a4a;
  --bg-assistant-msg: #232340;
  --bg-sidebar: #151528;
  --bg-overlay: rgba(0, 0, 0, 0.5);

  --text-primary: #e8e6e3;
  --text-secondary: #9a97a0;
  --text-muted: #6b6880;
  --text-accent: #c4b5fd;

  --border-color: #2a2a4a;
  --border-light: #333358;

  --header-height: 52px;
  --input-area-height: auto;
  --sidebar-width: 300px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  --font-stack: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "SF Mono", "Fira Code", "Fira Mono", "Roboto Mono", Consolas, monospace;
}

/* ---- Reset ---- */

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

html, body {
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-stack);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

ul {
  list-style: none;
}

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

/* ---- App Layout ---- */

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  position: relative;
}

/* ---- Header ---- */

#header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--header-height);
  padding: 0 16px;
  padding-top: var(--safe-top);
  background-color: var(--bg-primary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

#header h1 {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

#sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  color: var(--text-secondary);
  transition: background-color 0.15s, color 0.15s;
}

#sidebar-toggle:hover,
#sidebar-toggle:focus-visible {
  background-color: var(--bg-secondary);
  color: var(--text-primary);
}

/* ---- View Tabs ---- */

#view-tabs {
  display: flex;
  gap: 4px;
  margin-left: auto;
}

.view-tab {
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  transition: background-color 0.15s, color 0.15s;
}

.view-tab:hover,
.view-tab:focus-visible {
  background-color: var(--bg-secondary);
  color: var(--text-secondary);
}

.view-tab.active {
  background-color: var(--bg-secondary);
  color: var(--text-accent);
}

/* ---- Calendar View ---- */

#calendar-view[hidden] {
  display: none;
}

#calendar-view {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 0 8px 8px;
  --ec-bg-color: var(--bg-primary);
  --ec-text-color: var(--text-primary);
  --ec-border-color: var(--border-color);
  --ec-button-bg-color: var(--bg-secondary);
  --ec-button-border-color: var(--border-color);
  --ec-button-active-bg-color: var(--bg-tertiary);
  --ec-active-bg-color: var(--bg-tertiary);
  --ec-highlight-color: rgba(196, 181, 253, 0.08);
  --ec-today-bg-color: rgba(196, 181, 253, 0.06);
  --ec-now-indicator-color: #e74c3c;
  --ec-event-bg-color: var(--text-accent);
  --ec-event-color: var(--bg-primary);
  --ec-list-day-bg-color: var(--bg-secondary);
}

#ec {
  flex: 1;
  min-height: 0;
}

#calendar-view .ec-toolbar {
  padding: 8px 4px;
}

#calendar-view .ec-title {
  font-size: 16px;
  font-weight: 600;
}

#calendar-view .ec-button {
  color: var(--text-secondary);
  font-size: 13px;
}

#calendar-view .ec-button.ec-active {
  color: var(--text-accent);
}

#calendar-view .ec-day-head {
  font-size: 13px;
}

#calendar-view .ec-time {
  color: var(--text-muted);
  font-size: 12px;
}

/* ---- Event Popup ---- */

#event-popup {
  position: fixed;
  z-index: 50;
  max-width: 300px;
  padding: 14px 16px;
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-light);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  animation: message-in 0.15s ease-out;
}

#event-popup .popup-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 6px;
}

#event-popup .popup-time {
  font-size: 13px;
  color: var(--text-accent);
  margin-bottom: 4px;
}

#event-popup .popup-calendar {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

#event-popup .popup-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}

#event-popup .popup-description {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.45;
}

#event-popup .popup-location {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
  font-style: italic;
}

/* ---- Chat Area ---- */

#chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain;
  scroll-behavior: smooth;
  padding: 16px;
  padding-left: calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
}

/* Thin scrollbar */
#chat::-webkit-scrollbar {
  width: 4px;
}

#chat::-webkit-scrollbar-track {
  background: transparent;
}

#chat::-webkit-scrollbar-thumb {
  background-color: var(--border-color);
  border-radius: 4px;
}

#chat {
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}

#messages {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---- Message Bubbles ---- */

.message {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: break-word;
  animation: message-in 0.2s ease-out;
}

@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

.message-assistant {
  align-self: flex-start;
  background-color: var(--bg-assistant-msg);
  border-bottom-left-radius: 4px;
}

.message-timestamp {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-muted);
  text-align: right;
}

.message-user .message-timestamp {
  text-align: right;
}

.message-assistant .message-timestamp {
  text-align: left;
}

/* ---- Tool-Use Indicator ---- */

.tool-indicator {
  align-self: flex-start;
  padding: 6px 12px;
  font-size: 13px;
  font-style: italic;
  color: var(--text-muted);
  animation: message-in 0.2s ease-out;
}

/* ---- Markdown inside messages ---- */

.message-content strong {
  font-weight: 600;
  color: var(--text-primary);
}

.message-content em {
  font-style: italic;
}

.message-content code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  padding: 2px 5px;
  border-radius: 4px;
  background-color: rgba(255, 255, 255, 0.06);
}

.message-content pre {
  margin: 8px 0;
  padding: 12px;
  border-radius: 8px;
  background-color: rgba(0, 0, 0, 0.25);
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.45;
}

.message-content pre code {
  padding: 0;
  background: none;
  font-size: inherit;
}

.message-content ul,
.message-content ol {
  margin: 6px 0;
  padding-left: 20px;
}

.message-content ul {
  list-style: disc;
}

.message-content ol {
  list-style: decimal;
}

.message-content li {
  margin: 2px 0;
}

.message-content p {
  margin: 6px 0;
}

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

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

/* ---- Input Area ---- */

#input-area {
  position: sticky;
  bottom: 0;
  z-index: 10;
  padding: 8px 16px;
  padding-bottom: calc(8px + var(--safe-bottom));
  padding-left: calc(16px + var(--safe-left));
  padding-right: calc(16px + var(--safe-right));
  background-color: var(--bg-primary);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

/* ---- Attachment Preview ---- */

#attachment-preview {
  max-width: 720px;
  margin: 0 auto 6px;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 0;
}

#attachment-preview::-webkit-scrollbar {
  height: 3px;
}

#attachment-preview::-webkit-scrollbar-thumb {
  background-color: var(--border-color);
  border-radius: 3px;
}

.preview-thumb {
  position: relative;
  width: 60px;
  height: 60px;
  flex-shrink: 0;
  border-radius: 8px;
  overflow: hidden;
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
}

.preview-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.preview-file-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
}

.preview-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: none;
  padding: 0;
}

.preview-remove:hover {
  background-color: rgba(231, 76, 60, 0.8);
}

#input-wrapper {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background-color: var(--bg-input);
  border-radius: 12px;
  padding: 4px 4px 4px 4px;
  border: 1px solid var(--border-color);
  transition: border-color 0.15s;
}

#input-wrapper:focus-within {
  border-color: var(--border-light);
}

#message-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font-stack);
  font-size: 15px;
  line-height: 1.45;
  padding: 8px 0;
  resize: none;
  max-height: calc(1.45em * 6 + 16px);
  overflow-y: auto;
}

#message-input::placeholder {
  color: var(--text-muted);
}

#message-input::-webkit-scrollbar {
  width: 3px;
}

#message-input::-webkit-scrollbar-thumb {
  background-color: var(--border-color);
  border-radius: 3px;
}

#attach-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  color: var(--text-muted);
  transition: background-color 0.15s, color 0.15s;
  flex-shrink: 0;
}

#attach-btn:hover,
#attach-btn:focus-visible {
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
}

#send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  color: var(--text-muted);
  transition: background-color 0.15s, color 0.15s;
  flex-shrink: 0;
}

#send-btn:not(:disabled):hover,
#send-btn:not(:disabled):focus-visible {
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
}

#send-btn:not(:disabled) {
  color: var(--text-accent);
}

#send-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

#send-btn.stop-mode {
  color: #e74c3c;
}

#send-btn.stop-mode:hover,
#send-btn.stop-mode:focus-visible {
  background-color: rgba(231, 76, 60, 0.15);
  color: #e74c3c;
}

/* ---- Sidebar ---- */

#sidebar-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background-color: var(--bg-overlay);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

#sidebar-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 30;
  width: var(--sidebar-width);
  max-width: 80vw;
  background-color: var(--bg-sidebar);
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-bottom: var(--safe-bottom);
}

#sidebar.open {
  transform: translateX(0);
}

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

#sidebar-header h2 {
  font-size: 16px;
  font-weight: 600;
}

#new-chat-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-accent);
  transition: background-color 0.15s;
}

#new-chat-btn:hover,
#new-chat-btn:focus-visible {
  background-color: var(--bg-secondary);
}

#conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

#conversation-list::-webkit-scrollbar {
  width: 3px;
}

#conversation-list::-webkit-scrollbar-thumb {
  background-color: var(--border-color);
  border-radius: 3px;
}

.conversation-item {
  position: relative;
}

.conversation-item button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  text-align: left;
  font-size: 14px;
  color: var(--text-secondary);
  transition: background-color 0.15s, color 0.15s;
}

.conversation-item button:hover,
.conversation-item button:focus-visible {
  background-color: var(--bg-secondary);
  color: var(--text-primary);
}

.conversation-item.active button {
  background-color: var(--bg-secondary);
  color: var(--text-primary);
}

.conversation-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.conversation-delete {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border-radius: 6px;
  color: var(--text-muted);
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, background-color 0.15s;
}

.conversation-item:hover .conversation-delete,
.conversation-item button:focus-visible + .conversation-delete,
.conversation-delete:focus-visible {
  opacity: 1;
}

.conversation-delete:hover {
  color: #e57373;
  background-color: rgba(229, 115, 115, 0.1);
}

/* ---- Streaming dot animation ---- */

.streaming-cursor::after {
  content: "";
  display: inline-block;
  width: 6px;
  height: 14px;
  margin-left: 2px;
  background-color: var(--text-secondary);
  vertical-align: text-bottom;
  animation: blink 0.8s step-end infinite;
}

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

/* ---- Empty State ---- */

#empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  font-size: 15px;
  text-align: center;
  padding: 20px;
}

#empty-state h2 {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

/* ---- Message Attachments (Inline Images) ---- */

.message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.message-image {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  object-fit: contain;
}

.message-file-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border-radius: 6px;
  background-color: rgba(255, 255, 255, 0.06);
  color: var(--text-accent);
  font-size: 13px;
  text-decoration: none;
}

.message-file-link:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

/* ---- Focus styles ---- */

:focus-visible {
  outline: 2px solid var(--text-accent);
  outline-offset: 2px;
}

#message-input:focus-visible {
  outline: none;
}

/* ---- Desktop (wider than 768px) ---- */

@media (min-width: 768px) {
  .message {
    max-width: 70%;
  }

  #sidebar {
    width: 320px;
  }

  .conversation-delete {
    opacity: 0;
  }
}
