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

:root {
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-secondary: #64748b;
  --success: #16a34a;
  --danger: #dc2626;
  --radius: 12px;
  --header-h: 56px;
  --tabbar-h: 64px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

:root[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --border: #334155;
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro', 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.view { display: flex; flex-direction: column; height: 100dvh; }
[hidden] { display: none !important; }

/* --- Login --- */
.login-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 24px;
  max-width: 400px;
  margin: 0 auto;
  width: 100%;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-logo {
  width: 72px;
  height: 72px;
  background: var(--blue);
  color: white;
  border-radius: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 12px;
}

.login-header h1 {
  font-size: 22px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

.form-group input:focus,
.form-group select:focus {
  border-color: var(--blue);
}

.btn {
  display: block;
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}

.btn:active { opacity: 0.8; }

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

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

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
  margin-top: 24px;
}

.error-msg {
  background: #fef2f2;
  color: var(--danger);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 12px;
}

:root[data-theme="dark"] .error-msg { background: #450a0a; }

/* --- App Header --- */
.app-header {
  display: flex;
  align-items: center;
  height: var(--header-h);
  padding: 0 16px;
  padding-top: var(--safe-top);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.app-header h1 {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
}

.header-btn {
  background: none;
  border: none;
  color: var(--blue);
  padding: 8px;
  cursor: pointer;
  border-radius: 8px;
}

.header-btn:active { background: rgba(37, 99, 235, 0.1); }

.header-spacer { width: 38px; }

/* --- Tab Content --- */
.tab-content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* --- Portal --- */
#portal-frame {
  flex: 1;
  width: 100%;
  border: none;
}

.loading-indicator {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-secondary);
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* --- Bus --- */
.bus-toggle {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  gap: 8px;
}

.toggle-btn {
  flex: 1;
  padding: 8px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  background: transparent;
  color: var(--text-secondary);
  transition: all 0.15s;
}

.toggle-btn.active {
  background: var(--blue);
  color: white;
}

.bus-pane {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.direction-bar {
  display: flex;
  padding: 8px 16px;
  gap: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.dir-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface);
  color: var(--text-secondary);
  transition: all 0.15s;
}

.dir-btn.active {
  background: var(--blue);
  color: white;
  border-color: var(--blue);
}

.bus-date {
  padding: 8px 16px;
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
}

.stops-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px;
}

.stop-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}

.stop-order {
  width: 28px;
  height: 28px;
  background: var(--border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}

.stop-item.my-stop .stop-order {
  background: var(--blue);
  color: white;
}

.stop-info { flex: 1; }

.stop-name {
  font-size: 15px;
  font-weight: 500;
}

.stop-item.my-stop .stop-name { color: var(--blue); }

.stop-times {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.stop-arrival {
  font-size: 13px;
  font-weight: 600;
  color: var(--success);
  flex-shrink: 0;
}

.stop-arrival.empty { color: var(--text-secondary); }

.bus-refresh-btn {
  margin: 12px 16px;
  flex-shrink: 0;
}

.bus-map {
  flex: 1;
  min-height: 300px;
}

.map-status {
  padding: 8px 16px;
  text-align: center;
  font-size: 13px;
  color: var(--text-secondary);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.muted { text-align: center; color: var(--text-secondary); padding: 24px; }

/* --- Settings --- */
.settings-section {
  padding: 16px;
}

.settings-section h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.settings-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 15px;
}

.settings-item + .settings-item { border-top: none; }

.settings-item:first-of-type { border-radius: 10px 10px 0 0; }
.settings-item:last-of-type { border-radius: 0 0 10px 10px; }
.settings-item:only-of-type { border-radius: 10px; }

.link-item {
  text-decoration: none;
  color: var(--text);
  cursor: pointer;
}

.settings-add-btn { margin-top: 8px; }

.settings-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.account-item {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  gap: 12px;
}

.account-item + .account-item { border-top: none; }
.account-item:first-of-type { border-radius: 10px 10px 0 0; }
.account-item:last-of-type { border-radius: 0 0 10px 10px; }
.account-item:only-of-type { border-radius: 10px; }

.account-actions {
  margin-left: auto;
  display: flex;
  gap: 2px;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 16px;
  line-height: 1;
  padding: 6px 8px;
  cursor: pointer;
  border-radius: 6px;
}

.icon-btn:hover { background: var(--border); color: var(--text); }

.account-avatar {
  width: 36px;
  height: 36px;
  background: var(--blue);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

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

.account-name {
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.account-detail {
  font-size: 12px;
  color: var(--text-secondary);
}

.account-actions { display: flex; gap: 4px; }

.account-actions button {
  background: none;
  border: none;
  padding: 8px;
  cursor: pointer;
  color: var(--text-secondary);
  border-radius: 6px;
}

.account-actions button:active { background: var(--border); }

/* --- Tab Bar --- */
.tab-bar {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 0;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 10px;
  font-weight: 500;
  cursor: pointer;
  transition: color 0.15s;
}

.tab-btn.active { color: var(--blue); }

/* --- Account Switcher Overlay --- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
}

.overlay-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}

.switcher-panel {
  position: relative;
  width: 100%;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  max-height: 60vh;
  overflow-y: auto;
  animation: slide-up 0.25s ease-out;
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.switcher-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}

.switcher-header h3 { font-size: 17px; font-weight: 600; }

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
}

#switcher-list { padding: 8px 0; }

.switcher-item {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  gap: 12px;
  cursor: pointer;
  transition: background 0.1s;
}

.switcher-item:active { background: var(--border); }

.switcher-item.active { background: rgba(37, 99, 235, 0.08); }

.switcher-check {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.switcher-item.active .switcher-check {
  background: var(--blue);
  border-color: var(--blue);
  color: white;
}

.switcher-item.active .switcher-check::after { content: '\2713'; font-size: 12px; }

/* --- Edit Account Modal --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal {
  background: var(--surface);
  border-radius: 16px;
  width: 100%;
  max-width: 400px;
  max-height: 80vh;
  overflow-y: auto;
  animation: fade-in 0.15s ease-out;
}

@keyframes fade-in { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }

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

.modal-header h3 { font-size: 17px; font-weight: 600; }

.modal-body { padding: 16px 20px; }

.modal-footer {
  display: flex;
  gap: 8px;
  padding: 12px 20px 16px;
}

.modal-footer .btn { flex: 1; }
