/* Keuser Industries — utilitarian dark theme */

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --elevated: #2a2a2a;
  --border: #2a2a2a;
  --text: #e0e0e0;
  --text-muted: #888;
  --text-dim: #555;
  --red: #e54545;
  --amber: #d4a017;
  --blue: #4a9eff;
  --green: #3ddc84;
  --font-brand: 'Michroma', system-ui, sans-serif;
  --font-ui: system-ui, -apple-system, sans-serif;
  --font-mono: 'Courier New', Courier, monospace;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

.icon { display: inline-flex; align-items: center; vertical-align: middle; }
.icon svg { display: block; }

button {
  font-family: var(--font-ui);
  font-size: 13px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--elevated);
  color: var(--text);
  padding: 6px 14px;
  transition: opacity 150ms;
}
button:hover { opacity: 0.85; }
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--blue); color: #fff; border-color: var(--blue); }
button.danger { background: var(--red); color: #fff; border-color: var(--red); }
button.success { background: var(--green); color: #000; border-color: var(--green); }

input, textarea, select {
  font-family: var(--font-ui);
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  width: 100%;
  outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--blue); }
textarea { resize: vertical; min-height: 80px; font-family: var(--font-mono); }

/* Layout */
#app { display: flex; height: 100vh; }

/* Login */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
}
.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 40px;
  width: 320px;
}
.login-box .brand { font-family: var(--font-brand); text-align: center; margin-bottom: 24px; line-height: 1.2; }
.login-box .brand .brand-main { font-size: 22px; letter-spacing: 3px; display: block; }
.login-box .brand .brand-sub { font-size: 10px; letter-spacing: 5px; color: var(--text-muted); display: block; margin-top: 2px; }
.login-box input { margin-bottom: 12px; }
.login-box .error { color: var(--red); font-size: 12px; margin-bottom: 8px; }

/* Sidebar */
.sidebar {
  width: 200px;
  min-width: 200px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 0;
}
.sidebar .logo {
  font-family: var(--font-brand);
  padding: 0 16px 16px;
  border-bottom: 1px solid var(--border);
  line-height: 1.2;
}
.sidebar .logo .brand-main {
  font-size: 15px;
  letter-spacing: 2px;
  display: block;
}
.sidebar .logo .brand-sub {
  font-size: 8px;
  letter-spacing: 4px;
  color: var(--text-muted);
  display: block;
  margin-top: 1px;
}
.sidebar nav { flex: 1; padding: 8px 0; }
.sidebar nav a {
  display: block;
  padding: 8px 16px;
  color: var(--text-muted);
  font-size: 13px;
  text-decoration: none;
  border-left: 2px solid transparent;
}
.sidebar nav a:hover { color: var(--text); background: var(--elevated); }
.sidebar nav a.active {
  color: var(--text);
  border-left-color: var(--blue);
  background: var(--elevated);
}
.sidebar nav a .badge {
  float: right;
  background: var(--red);
  color: #fff;
  font-size: 11px;
  padding: 0 6px;
  min-width: 18px;
  text-align: center;
}
.sidebar .logout-btn {
  margin: 8px 16px;
  font-size: 12px;
  text-align: center;
}

/* Main content */
.main { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.view { flex: 1; opacity: 1; transition: opacity 150ms; }
.view-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.view-header h2 { font-size: 16px; font-weight: 600; }
.view-body { padding: 16px 24px; }

/* Email list */
.email-list { display: flex; flex-direction: column; }
.email-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: opacity 150ms;
}
.email-row:hover { background: var(--surface); }
.email-row.unread { font-weight: 600; }
.email-row .score-badge {
  font-size: 11px;
  font-weight: 700;
  min-width: 28px;
  text-align: center;
  padding: 2px 4px;
  border: 1px solid;
}
.email-row .score-badge.urgent { color: var(--red); border-color: var(--red); }
.email-row .score-badge.high { color: var(--amber); border-color: var(--amber); }
.email-row .score-badge.normal { color: var(--text-muted); border-color: var(--border); }
.email-row .sender { width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.email-row .subject { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.email-row .account-label { font-size: 11px; color: var(--text-dim); width: 100px; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.email-row .time { font-size: 11px; color: var(--text-muted); width: 60px; text-align: right; white-space: nowrap; }
.email-row .unread-dot { width: 6px; height: 6px; background: var(--blue); flex-shrink: 0; }

/* Email detail */
.email-detail { max-width: none; }
.email-detail .back-btn { margin-bottom: 16px; }
.email-detail .meta { margin-bottom: 16px; }
.email-detail .meta h3 { font-size: 16px; margin-bottom: 4px; }
.email-detail .meta .info { font-size: 12px; color: var(--text-muted); }

/* Two-column split layout */
.email-split {
  display: flex;
  gap: 20px;
}
.email-ai-panel {
  flex: 0 0 340px;
  min-width: 0;
}
.email-body-panel {
  flex: 1;
  min-width: 0;
}
.email-detail .summary-box {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 12px;
  margin-bottom: 16px;
}
.email-detail .summary-box .label { font-size: 11px; color: var(--blue); text-transform: uppercase; margin-bottom: 4px; }
.email-detail .body-text {
  font-family: var(--font-mono);
  font-size: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: calc(100vh - 200px);
  overflow-y: auto;
}
.email-detail .replies { margin-bottom: 16px; }
.email-detail .replies .label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; margin-bottom: 8px; }
.email-detail .replies button { margin-right: 8px; margin-bottom: 8px; }
.email-detail .custom-reply { margin-bottom: 16px; }
.email-detail .custom-reply textarea { margin-bottom: 8px; }

/* Reply confirmation */
.reply-confirm {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 16px;
  margin-top: 12px;
}
.reply-confirm .preview {
  font-family: var(--font-mono);
  font-size: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  margin-bottom: 12px;
  max-height: 200px;
  overflow-y: auto;
}
.reply-confirm .actions { display: flex; gap: 8px; }

/* Filters */
.filters {
  display: flex;
  gap: 8px;
  align-items: center;
}
.filters select { width: auto; }

/* Calendar */
.calendar-layout { display: flex; gap: 16px; }
.mini-calendar { width: 260px; flex-shrink: 0; }
.mini-calendar table { width: 100%; border-collapse: collapse; }
.mini-calendar th, .mini-calendar td {
  text-align: center;
  padding: 6px;
  font-size: 12px;
}
.mini-calendar th { color: var(--text-muted); }
.mini-calendar td { cursor: pointer; }
.mini-calendar td:hover { background: var(--elevated); }
.mini-calendar td.today { color: var(--blue); font-weight: 700; }
.mini-calendar td.has-events { border-bottom: 2px solid var(--blue); }
.mini-calendar .month-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 13px;
}
.week-view { flex: 1; min-width: 0; }
.event-list { display: flex; flex-direction: column; gap: 4px; }
.event-item {
  padding: 8px 12px;
  border-left: 3px solid var(--blue);
  background: var(--surface);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
}
.event-item:hover { background: var(--elevated); }
.event-item .event-time { font-size: 12px; color: var(--text-muted); width: 120px; flex-shrink: 0; }
.event-item .event-title { font-size: 13px; flex: 1; }
.event-item .event-account { font-size: 11px; color: var(--text-dim); }
.day-header { font-size: 12px; color: var(--text-muted); padding: 12px 0 4px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }

.event-detail { max-width: 600px; }
.event-detail .field { margin-bottom: 12px; }
.event-detail .field label { display: block; font-size: 11px; color: var(--text-muted); text-transform: uppercase; margin-bottom: 2px; }
.event-form { max-width: 500px; }
.event-form .field { margin-bottom: 12px; }
.event-form .field label { display: block; font-size: 11px; color: var(--text-muted); text-transform: uppercase; margin-bottom: 4px; }
.event-form .actions { display: flex; gap: 8px; margin-top: 16px; }

/* Agent cards */
.agent-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; margin-bottom: 24px; }
.agent-card {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 16px;
}
.agent-card .card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.agent-card .card-header .label { font-weight: 600; font-size: 14px; }
.agent-card .email { font-size: 12px; color: var(--text-muted); margin-bottom: 8px; }
.agent-card .provider-badge { font-size: 11px; padding: 1px 6px; border: 1px solid var(--border); color: var(--text-muted); }
.agent-card .status {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  padding: 2px 8px;
  border: 1px solid;
}
.agent-card .status.active { color: var(--green); border-color: var(--green); }
.agent-card .status.paused { color: var(--text-muted); border-color: var(--border); }
.agent-card .status.error { color: var(--red); border-color: var(--red); }
.agent-card .stats { font-size: 12px; color: var(--text-muted); margin: 8px 0; }
.agent-card .error-msg { font-size: 12px; color: var(--red); margin: 4px 0; font-family: var(--font-mono); }
.agent-card .last-polled { font-size: 11px; color: var(--text-dim); }
.agent-card .card-actions { display: flex; gap: 8px; margin-top: 12px; }

/* Agent log */
.agent-log-list { max-height: 500px; overflow-y: auto; }
.log-entry {
  display: flex;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-family: var(--font-mono);
  align-items: baseline;
}
.log-entry .log-time { color: var(--text-dim); width: 160px; flex-shrink: 0; }
.log-entry .log-account { color: var(--text-muted); width: 120px; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-entry .log-type {
  font-size: 10px;
  padding: 1px 6px;
  border: 1px solid;
  text-transform: uppercase;
  width: 80px;
  text-align: center;
  flex-shrink: 0;
}
.log-entry .log-type.poll { color: var(--text-muted); border-color: var(--border); }
.log-entry .log-type.triage { color: var(--blue); border-color: var(--blue); }
.log-entry .log-type.notification { color: var(--amber); border-color: var(--amber); }
.log-entry .log-type.error { color: var(--red); border-color: var(--red); }
.log-entry .log-type.digest { color: var(--green); border-color: var(--green); }
.log-entry .log-msg { flex: 1; word-break: break-word; }

/* Account list */
.account-list { display: flex; flex-direction: column; gap: 8px; }
.account-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.account-item .account-info { display: flex; align-items: center; gap: 12px; }
.account-item .account-email { font-size: 12px; color: var(--text-muted); }
.account-item .actions { display: flex; gap: 8px; }
.add-buttons { display: flex; gap: 8px; margin-top: 16px; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  animation: modal-fade-in 150ms ease;
}
@keyframes modal-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  width: 90%;
  max-width: 380px;
  padding: 24px;
}
.modal h3 {
  font-size: 15px;
  margin-bottom: 16px;
  font-weight: 600;
}
.modal .modal-field {
  margin-bottom: 14px;
}
.modal .modal-field label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}
.modal .modal-field input,
.modal .modal-field select {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 13px;
  outline: none;
}
.modal .modal-field input:focus,
.modal .modal-field select:focus {
  border-color: var(--blue);
}
.modal .modal-field input::placeholder {
  color: var(--text-dim);
}
.modal .modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 20px;
}

/* Settings */
.settings-form { max-width: 400px; }
.settings-form .field { margin-bottom: 16px; }
.settings-form .field label { display: block; font-size: 12px; color: var(--text-muted); text-transform: uppercase; margin-bottom: 4px; }
.settings-form .field input[type="range"] { padding: 0; }
.settings-form .range-value { font-size: 13px; font-weight: 600; margin-left: 8px; }

/* Spinner */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--blue);
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Empty state */
.empty { color: var(--text-dim); font-size: 13px; padding: 40px; text-align: center; }

/* Mobile — iPhone safe areas + usability */
@media (max-width: 899px) {
  .sidebar { display: none; }

  /* Safe area support for newer iPhones (Dynamic Island, notch, home indicator) */
  .view-header {
    padding: 12px 16px;
    padding-top: max(12px, env(safe-area-inset-top, 0px));
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
  }
  .view-body {
    padding: 16px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
  }

  /* Bottom tab bar — lifted above home indicator */
  .mobile-tabs {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: 100;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
  .mobile-tabs a {
    flex: 1;
    text-align: center;
    padding: 8px 0 4px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 20px;
    -webkit-tap-highlight-color: transparent;
    min-height: 48px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
  }
  .mobile-tabs a .tab-label {
    font-size: 9px;
    display: block;
    letter-spacing: 0.3px;
    line-height: 1;
  }
  .mobile-tabs a.active { color: var(--blue); }

  /* Main content — clear the taller tab bar + safe area */
  .main {
    padding-bottom: calc(60px + env(safe-area-inset-bottom, 0px));
  }

  /* Login screen — keep centered and respect safe areas */
  .login-screen {
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  }
  .login-box {
    width: calc(100% - 32px);
    max-width: 320px;
    padding: 32px 24px;
  }

  /* Calendar */
  .calendar-layout { flex-direction: column; }
  .mini-calendar { width: 100%; }

  /* Email list — larger touch targets, better readability */
  .email-row {
    padding: 14px 16px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    gap: 10px;
    min-height: 56px;
  }
  .email-row .account-label { display: none; }
  .email-row .sender { width: 100px; font-size: 14px; }
  .email-row .subject { font-size: 14px; }
  .email-row .time { font-size: 12px; }
  .email-row .score-badge { font-size: 12px; min-width: 32px; padding: 3px 6px; }

  /* Email detail — breathable on mobile */
  .email-detail { padding: 0; }
  .email-detail .meta h3 { font-size: 18px; }
  .email-split { flex-direction: column; }
  .email-ai-panel { flex: none; }
  .email-detail .body-text { font-size: 14px; max-height: none; }
  .email-detail .replies button {
    padding: 10px 16px;
    font-size: 14px;
    margin-bottom: 8px;
    width: 100%;
    margin-right: 0;
  }
  .email-detail .custom-reply textarea { font-size: 16px; min-height: 100px; }

  /* Reply confirmation — full width actions */
  .reply-confirm .actions {
    flex-direction: column;
  }
  .reply-confirm .actions button {
    width: 100%;
    padding: 12px;
    font-size: 14px;
  }

  /* Buttons — larger touch targets on mobile */
  button {
    padding: 10px 16px;
    font-size: 14px;
    min-height: 44px;
  }

  /* Inputs — prevent iOS zoom (needs 16px+), larger touch targets */
  input, textarea, select {
    font-size: 16px;
    padding: 10px 12px;
    min-height: 44px;
  }

  /* Agent cards — single column on mobile */
  .agent-cards { grid-template-columns: 1fr; }
  .agent-card .card-actions button { flex: 1; }

  /* Account list — stack actions on smaller screens */
  .account-item { flex-direction: column; align-items: stretch; gap: 12px; }
  .account-item .actions { justify-content: flex-end; }

  /* Settings — full width */
  .settings-form { max-width: 100%; }

  /* Filters — wrap on mobile */
  .filters { flex-wrap: wrap; }
  .filters select { flex: 1; min-width: 120px; }

  /* Event form — full width */
  .event-form { max-width: 100%; }

  /* Log entries — wrap on mobile instead of horizontal scroll */
  .log-entry { flex-wrap: wrap; gap: 4px 8px; }
  .log-entry .log-time { width: auto; }
  .log-entry .log-account { width: auto; }
  .log-entry .log-msg { width: 100%; }
}

@media (min-width: 900px) {
  .mobile-tabs { display: none; }
}

/* Splash screen */
.ki-splash {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.4s ease;
}
.ki-splash.visible { opacity: 1; }
.ki-splash.fade-out { opacity: 0; transition: opacity 0.5s ease; }
.ki-splash-brand {
  font-family: var(--font-brand);
  text-align: center;
  animation: splashIn 0.8s ease-out both;
}
.ki-splash-name {
  font-size: 42px;
  letter-spacing: 8px;
  color: var(--text);
}
.ki-splash-sub {
  font-size: 14px;
  letter-spacing: 10px;
  color: var(--text-muted);
  margin-top: 4px;
  animation: splashSubIn 0.6s ease-out 0.4s both;
}
@keyframes splashIn {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes splashSubIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Footer watermark */
.ki-footer {
  font-family: var(--font-brand);
  text-align: center;
  padding: 24px 16px 16px;
  margin-top: auto;
  opacity: 0.3;
  line-height: 1.3;
}
.ki-footer-name {
  display: block;
  font-size: 10px;
  letter-spacing: 3px;
  color: var(--text-muted);
}
.ki-footer-sub {
  display: block;
  font-size: 7px;
  letter-spacing: 5px;
  color: var(--text-dim);
}
