:root {
  --bg: #f4f7f6;
  --panel: #ffffff;
  --panel-soft: #f8fbfa;
  --text: #13201d;
  --muted: #66736f;
  --border: #d9e3df;
  --teal: #0f6d63;
  --teal-dark: #0a5049;
  --amber: #b87512;
  --green: #16834a;
  --red: #b33b36;
  --shadow: 0 18px 45px rgba(23, 45, 40, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1480px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0;
}

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

.topbar h1 {
  margin: 0;
  font-size: clamp(28px, 4vw, 44px);
  line-height: 1.03;
  letter-spacing: 0;
}

.topbar p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 15px;
  overflow-wrap: anywhere;
}

.topbar-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.address-search-control {
  display: grid;
  gap: 4px;
  min-width: 260px;
}

.address-search-control span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.address-search-control select {
  min-height: 44px;
}

.mobile-tabs {
  display: none;
}

.workspace {
  display: grid;
  grid-template-columns: 350px minmax(420px, 1fr) 390px;
  gap: 16px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

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

.panel-header > * {
  min-width: 0;
}

.panel-header h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}

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

.file-drop {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  margin: 12px 0;
  border: 1px solid var(--teal);
  border-radius: 7px;
  background: var(--teal);
  color: #fff;
  cursor: pointer;
  overflow: hidden;
  font-weight: 800;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}

.step-action {
  position: relative;
  justify-content: center;
  padding-left: 56px;
  padding-right: 56px;
}

.step-action::before {
  content: attr(data-step);
  display: inline-grid;
  place-items: center;
  position: absolute;
  left: 18px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  border: 1px solid rgba(255, 255, 255, 0.85);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  line-height: 1;
}

.file-drop:hover {
  background: var(--teal-dark);
  border-color: var(--teal-dark);
}

.file-drop span {
  color: inherit;
  font-weight: inherit;
}

.file-drop input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.planner-action-stack {
  display: grid;
  gap: 8px;
  margin: -2px 0 14px;
}

.planner-action-stack .primary-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  border-color: transparent;
  color: #fff;
}

.planner-action-stack .action-optimize {
  background: #238a67;
}

.planner-action-stack .action-optimize:hover {
  background: #1b7558;
}

.planner-action-stack .action-print {
  background: #13785f;
}

.planner-action-stack .action-print:hover {
  background: #0f654f;
}

.planner-action-stack .action-release {
  background: #075f50;
}

.planner-action-stack .action-release:hover {
  background: #044d42;
}

.planner-action-stack .primary-btn:disabled {
  opacity: 0.58;
  cursor: not-allowed;
}

.field-stack {
  display: grid;
  gap: 12px;
}

.field-group {
  display: grid;
  gap: 6px;
}

.start-point-tools,
.inline-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: end;
}

.inline-field input {
  width: 100%;
}

.icon-btn {
  width: 42px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}

.optimization-status-card {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-soft);
}

.optimization-status-card h3 {
  margin: 0;
  color: var(--text);
  font-size: 14px;
  line-height: 1.25;
}

.optimization-status {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  font-size: 13px;
}

.optimization-status .status-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: var(--red);
  box-shadow: 0 0 0 4px rgba(179, 59, 54, 0.12);
}

.optimization-status.available {
  color: var(--green);
}

.optimization-status.available .status-dot {
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(22, 131, 74, 0.12);
}

.optimization-status.unavailable {
  color: var(--red);
}

.optimization-status-card p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

label {
  display: grid;
  gap: 6px;
  color: #40504b;
  font-size: 13px;
  font-weight: 700;
}

input,
select {
  min-width: 0;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  padding: 0 11px;
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 109, 99, 0.14);
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 12px 0 14px;
}

.primary-btn,
.ghost-btn {
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 0 14px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}

.primary-btn {
  background: var(--teal);
  color: #fff;
}

.primary-btn:hover {
  background: var(--teal-dark);
}

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

.ghost-btn:hover {
  border-color: #a9bbb5;
}

.ghost-btn.danger {
  color: var(--red);
}

.wide {
  width: 100%;
}

.tour-start-btn {
  margin-top: 10px;
}

.notice {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.notice.compact {
  margin-top: 8px;
}

.route-feedback {
  margin: 8px 0 10px;
}

.route-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.route-metrics div {
  display: grid;
  gap: 3px;
  min-height: 82px;
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-soft);
}

.route-metrics strong {
  color: #40504b;
  font-size: 12px;
}

.route-metrics span {
  color: var(--text);
  font-size: 20px;
  font-weight: 900;
}

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

.stops-panel {
  min-height: 680px;
}

.stops-list {
  display: grid;
  gap: 8px;
  max-height: calc(100vh - 180px);
  overflow: auto;
  padding-right: 4px;
}

.stops-list.empty {
  min-height: 280px;
  place-items: center;
}

.empty-state {
  display: grid;
  gap: 6px;
  color: var(--muted);
  text-align: center;
  padding: 22px;
}

.empty-state strong {
  color: var(--text);
}

.stop-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) 104px 142px;
  gap: 10px;
  align-items: center;
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}

.stop-index {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #e7f1ef;
  color: var(--teal-dark);
  font-weight: 900;
  font-size: 13px;
}

.stop-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.stop-main strong,
.stop-address,
.stop-meta {
  overflow-wrap: anywhere;
}

.stop-main strong {
  font-size: 14px;
}

.stop-address {
  color: #40504b;
  font-size: 13px;
}

.stop-meta {
  color: var(--muted);
  font-size: 12px;
}

.priority-high {
  border-left: 4px solid var(--amber);
}

.priority-low {
  opacity: 0.82;
}

.status-delivered .stop-index {
  background: #dff4e7;
  color: var(--green);
}

.status-missed .stop-index,
.status-declined .stop-index {
  background: #f8e7e5;
  color: var(--red);
}

.current-stop {
  min-height: 380px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: linear-gradient(180deg, #f9fcfb, #fff);
  padding: 18px;
}

.delivery-card {
  display: grid;
  gap: 14px;
}

.delivery-position {
  color: var(--teal-dark);
  font-size: 13px;
  font-weight: 900;
}

.delivery-card h3 {
  margin: 0;
  font-size: 25px;
  line-height: 1.08;
}

.delivery-card address {
  color: #40504b;
  font-style: normal;
  line-height: 1.45;
}

.delivery-actions {
  display: grid;
  gap: 9px;
}

.map-actions {
  display: grid;
  gap: 8px;
}

.status-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.status-actions .status-btn {
  min-height: 58px;
  white-space: normal;
}

.status-btn[data-status="delivered"] {
  border-color: rgba(22, 131, 74, 0.25);
  color: var(--green);
}

.status-btn[data-status="missed"],
.status-btn[data-status="declined"],
.status-btn[data-status="open"] {
  border-color: rgba(179, 59, 54, 0.22);
  color: var(--red);
}

.summary-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 14px;
}

.summary-strip div {
  display: grid;
  gap: 3px;
  place-items: center;
  min-height: 76px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-soft);
}

.summary-strip span {
  font-size: 26px;
  font-weight: 900;
}

.summary-strip small {
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.history-panel {
  grid-column: 1 / -1;
}

.history-summary {
  margin: 0 0 14px;
}

body.role-courier .address-search-control,
body.role-courier #exportBtn,
body.role-courier .planner-panel,
body.role-courier .stops-panel {
  display: none;
}

body.role-dispatch #startTourBtn,
body.role-dispatch .courier-panel {
  display: none;
}

body.role-courier .courier-panel {
  grid-column: 1 / 2;
}

body.role-courier .history-panel {
  grid-column: 2 / -1;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-list.empty {
  min-height: 160px;
  place-items: center;
}

.history-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
}

.history-item summary {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 13px 14px;
  cursor: pointer;
  font-weight: 800;
}

.history-result {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-align: right;
}

.history-detail {
  display: grid;
  gap: 12px;
  padding: 0 14px 14px;
}

.history-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}

.history-stops {
  display: grid;
  gap: 7px;
}

.history-stop {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(200px, 1.5fr) minmax(130px, auto);
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--border);
  border-left: 4px solid #9bb1aa;
  border-radius: 7px;
  background: var(--panel-soft);
}

.history-stop strong,
.history-stop span,
.history-stop em {
  overflow-wrap: anywhere;
}

.history-stop span {
  color: #40504b;
  font-size: 13px;
}

.history-stop em {
  color: var(--muted);
  font-size: 13px;
  font-style: normal;
  text-align: right;
}

.history-stop.status-delivered {
  border-left-color: var(--green);
}

.history-stop.status-missed,
.history-stop.status-declined {
  border-left-color: var(--red);
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 320px 1fr;
  }

  .courier-panel {
    grid-column: 1 / -1;
  }
}

@media (max-width: 760px) {
  .app-shell {
    width: 100%;
    padding: 12px 10px 76px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    max-width: calc(100vw - 20px);
  }

  .topbar p,
  .notice {
    max-width: 320px;
  }

  .topbar-actions,
  .topbar-actions button,
  .topbar-actions .address-search-control {
    width: 100%;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .topbar-actions .save-state {
    grid-column: 1 / -1;
  }

  #exportBtn {
    grid-column: 1 / -1;
  }

  .address-search-control {
    grid-column: 1 / -1;
  }

  .workspace {
    grid-template-columns: 1fr;
    max-width: calc(100vw - 20px);
  }

  .mobile-tabs {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 20;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0;
    border-top: 1px solid var(--border);
    background: #fff;
    box-shadow: 0 -10px 24px rgba(23, 45, 40, 0.08);
  }

  body.role-courier .mobile-tabs {
    grid-template-columns: repeat(2, 1fr);
  }

  body.role-courier .mobile-tabs button[data-view="plan"],
  body.role-courier .mobile-tabs button[data-view="route"] {
    display: none;
  }

  body.role-dispatch .mobile-tabs {
    grid-template-columns: repeat(3, 1fr);
  }

  body.role-dispatch .mobile-tabs button[data-view="delivery"] {
    display: none;
  }

  .mobile-tabs button {
    min-height: 58px;
    border: 0;
    border-top: 3px solid transparent;
    background: #fff;
    color: var(--muted);
    font-weight: 800;
  }

  .mobile-tabs button.active {
    border-top-color: var(--teal);
    color: var(--teal-dark);
  }

  body.mobile-view-plan .stops-panel,
  body.mobile-view-plan .courier-panel,
  body.mobile-view-plan .history-panel,
  body.mobile-view-route .planner-panel,
  body.mobile-view-route .courier-panel,
  body.mobile-view-route .history-panel,
  body.mobile-view-delivery .planner-panel,
  body.mobile-view-delivery .stops-panel,
  body.mobile-view-delivery .history-panel,
  body.mobile-view-history .planner-panel,
  body.mobile-view-history .stops-panel,
  body.mobile-view-history .courier-panel {
    display: none;
  }

  body.role-courier.mobile-view-delivery .history-panel,
  body.role-courier.mobile-view-history .courier-panel {
    display: none;
  }

  body.role-courier.mobile-view-delivery .courier-panel,
  body.role-courier.mobile-view-history .history-panel {
    display: block;
  }

  body.role-dispatch.mobile-view-delivery .planner-panel {
    display: block;
  }

  .panel {
    padding: 14px;
    overflow: hidden;
    width: 100%;
    max-width: calc(100vw - 20px);
  }

  .panel-header {
    flex-wrap: wrap;
  }

  .save-state {
    display: none;
  }

  .file-drop input {
    font-size: 12px;
  }

  .settings-grid,
  .start-point-tools,
  .inline-field,
  .route-metrics {
    grid-template-columns: 1fr;
  }

  .summary-strip {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .status-actions {
    grid-template-columns: 1fr 1fr;
  }

  .history-item summary,
  .history-meta,
  .history-stop {
    grid-template-columns: 1fr;
  }

  .history-result,
  .history-stop em {
    text-align: left;
  }

  .stop-row {
    grid-template-columns: 34px minmax(0, 1fr);
  }

  .priority-select,
  .status-select {
    grid-column: 2;
  }

  .stops-panel {
    min-height: auto;
  }

  .stops-list {
    max-height: none;
  }
}
body.auth-pending .app-shell,
body.auth-required .app-shell,
body.authenticated .login-view {
  display: none;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: #eef4f1;
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  padding: 24px;
  border: 1px solid #d6dfda;
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 20px 60px rgba(31, 48, 42, 0.12);
}

.login-card h1 {
  margin: 0;
  font-size: 1.7rem;
}

.login-card p {
  margin: 0;
}

.print-route-sheet {
  display: none;
}

@media print {
  body {
    background: #fff;
  }

  .login-view,
  .app-shell {
    display: none !important;
  }

  .print-route-sheet {
    display: block;
    padding: 0;
    color: #111;
    font: 8.5pt/1.2 Arial, sans-serif;
  }

  .print-route-sheet header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: baseline;
    border-bottom: 1px solid #111;
    margin-bottom: 5px;
    padding-bottom: 3px;
  }

  .print-route-sheet h1 {
    font-size: 13pt;
  }

  .print-route-sheet h1,
  .print-route-sheet p {
    margin: 0;
  }

  .print-route-meta {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    margin-bottom: 5px;
  }

  .print-route-meta div {
    border: 1px solid #999;
    padding: 3px 5px;
  }

  .print-route-meta strong,
  .print-route-meta span {
    display: inline;
  }

  .print-route-map {
    margin: 0 0 5px;
    break-inside: avoid;
  }

  .print-route-map img {
    display: block;
    width: 100%;
    height: 72mm;
    object-fit: contain;
    background: #f5f7f6;
    border: 1px solid #999;
  }

  .print-route-map figcaption {
    margin-top: 1px;
    color: #333;
    font-size: 7pt;
  }

  .print-route-list {
    display: grid;
    gap: 0;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .print-route-list li {
    display: grid;
    grid-template-columns: 28px 1fr;
    gap: 6px;
    break-inside: avoid;
    border-bottom: 0.5px solid #bbb;
    padding: 3px 0;
  }

  .print-route-index {
    display: block;
    width: auto;
    height: auto;
    border: 0;
    border-radius: 0;
    font-weight: 700;
    line-height: 1.2;
    text-align: right;
  }

  .print-route-list strong,
  .print-route-list p,
  .print-route-list small {
    display: block;
    margin: 0;
  }

  .print-route-list small {
    color: #333;
    margin-top: 1px;
    font-size: 7.5pt;
  }
}
