:root {
  --bg: var(--tg-theme-bg-color, #f3f4f6);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #12161c);
  --muted: var(--tg-theme-hint-color, #6b7280);
  --accent: var(--tg-theme-button-color, #1f6feb);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --border: rgba(128, 128, 128, 0.24);
  --danger: #c0392b;
  --ok: #1e8e5a;
  --warn: #a86400;
  --radius: 16px;
}

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  letter-spacing: -0.01em;
}

h2 {
  font-size: 17px;
}

h3 {
  font-size: 16px;
}

.muted {
  color: var(--muted);
  font-size: 13px;
}

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

.right {
  margin-left: auto;
  text-align: right;
  flex: 0 0 auto;
}

#app {
  max-width: 980px;
  margin: 0 auto;
  padding: 18px 16px calc(36px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.identity {
  min-width: 0;
}

.identity h1 {
  margin-bottom: 2px;
}

.identity .muted {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 4px;
  margin-bottom: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  flex: 0 0 auto;
  border: 0;
  border-radius: 999px;
  padding: 8px 14px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.tab.is-active {
  background: var(--accent);
  color: var(--accent-text);
}

.panel {
  display: block;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.card-title {
  font-weight: 650;
  word-break: break-word;
}

.row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.row.between {
  justify-content: space-between;
}

.row.wrap {
  flex-wrap: wrap;
}

.row.gap {
  gap: 6px;
}

.stack-tight {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--muted);
}

.field.inline {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  margin: 0;
}

.field span {
  font-weight: 600;
}

input,
select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

.field.inline select {
  width: auto;
}

input:focus,
select:focus,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 14px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.btn.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.btn.danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}

.btn.small {
  padding: 6px 10px;
  font-size: 13px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.inline-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.inline-form input[type="text"] {
  flex: 1 1 220px;
  width: auto;
}

.inline-form .btn {
  flex: 0 0 auto;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
}

.check input {
  width: auto;
}

.disclosure {
  margin-bottom: 14px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.disclosure summary {
  cursor: pointer;
  font-weight: 600;
}

.disclosure .inline-form {
  margin: 12px 0 0;
}

.status {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(128, 128, 128, 0.16);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}

.status-new,
.status-assigned {
  background: rgba(31, 111, 235, 0.14);
  color: #1f6feb;
}

.status-in_progress,
.status-accepted,
.status-arrived,
.status-reserved {
  background: rgba(168, 100, 0, 0.15);
  color: var(--warn);
}

.status-completed,
.status-available {
  background: rgba(30, 142, 90, 0.15);
  color: var(--ok);
}

.status-cancelled,
.status-failed {
  background: rgba(192, 57, 43, 0.14);
  color: var(--danger);
}

.status-charging {
  background: rgba(126, 87, 194, 0.16);
  color: #7e57c2;
}

.status-on_trip {
  background: rgba(31, 111, 235, 0.14);
  color: #1f6feb;
}

.legs {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.leg {
  padding: 8px 0 8px 10px;
  border-left: 3px solid var(--border);
}

.leg:has(.status-completed) {
  border-left-color: var(--ok);
}

.leg:has(.status-assigned),
.leg:has(.status-accepted),
.leg:has(.status-arrived) {
  border-left-color: var(--accent);
}

.leg-line {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}

.leg-route {
  font-weight: 600;
  font-size: 14px;
  word-break: break-word;
}

.leg-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12.5px;
}

.card-footer {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}

.empty {
  padding: 28px 18px;
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.queue-point {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.queue-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.queue-entry {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.queue-entry:last-child {
  border-bottom: 0;
}

.position {
  flex: 0 0 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 12px;
  font-weight: 700;
}

.route-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

.route-actions {
  display: flex;
  gap: 6px;
  flex: 0 0 auto;
}

.segments {
  margin-top: 12px;
  border-top: 1px solid var(--border);
}

.segment-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.segment-row:last-child {
  border-bottom: 0;
}

.segment-main {
  min-width: 0;
}

.segment-title {
  font-weight: 600;
  font-size: 14px;
  word-break: break-word;
}

.segment-meta {
  color: var(--muted);
  font-size: 12.5px;
  margin-top: 1px;
}

.segment-actions {
  display: flex;
  gap: 4px;
  flex: 0 0 auto;
}

.add-segment {
  margin-top: 10px;
}

.gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.gate-card {
  width: min(420px, 100%);
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 30px 24px;
}

.gate-card .logo {
  font-size: 44px;
  line-height: 1;
  margin-bottom: 12px;
}

.gate-card h1 {
  margin-bottom: 10px;
}

.gate-card p {
  color: var(--muted);
  margin-bottom: 18px;
}

.gate-card .btn {
  width: 100%;
}

dialog {
  width: calc(100% - 32px);
  max-width: 420px;
  padding: 0;
  border: 0;
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.48);
}

dialog form {
  padding: 20px;
}

dialog h2 {
  margin-bottom: 16px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  max-width: min(420px, calc(100% - 32px));
  padding: 10px 16px;
  border-radius: 12px;
  background: #1f2933;
  color: #fff;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 20;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast.error {
  background: var(--danger);
}

@media (max-width: 520px) {
  #app {
    padding-top: 14px;
  }

  .card {
    padding: 12px;
  }

  .leg-meta {
    flex-direction: column;
    gap: 0;
  }

  .route-head {
    flex-direction: column;
  }

  .route-actions {
    align-self: stretch;
  }

  .route-actions .btn {
    flex: 1;
  }
}
