/* =============================================
   RunRoute — Mobile-First Stylesheet
   ============================================= */

/* --- CSS Variables --- */
:root {
  --color-primary: #4285F4;
  --color-surface: #ffffff;
  --color-bg: #f8f9fa;
  --color-text: #202124;
  --color-text-secondary: #5f6368;
  --color-border: #dadce0;
  --color-error: #d93025;
  --color-success: #34a853;

  --route-1: #4285F4; /* Blue */
  --route-2: #EA4335; /* Red */
  --route-3: #34A853; /* Green */

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-pill: 100px;

  --shadow-sm: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,.10), 0 2px 4px rgba(0,0,0,.06);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.12);

  --header-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* --- Reset & Base --- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Segoe UI', 'Hiragino Sans', 'Hiragino Kaku Gothic ProN',
               'Noto Sans JP', sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  min-height: 100dvh;
  overscroll-behavior: none;
}

/* --- Header --- */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--color-primary);
  box-shadow: var(--shadow-sm);
  height: var(--header-h);
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 100%;
  padding: 0 16px;
}

.header-logo {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
}

.header-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.02em;
}

/* --- Main --- */
.main {
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: calc(24px + var(--safe-bottom));
}

/* --- Setup Section --- */
.setup-section {
  padding: 20px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Location Status */
.location-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.875rem;
  color: var(--color-text-secondary);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  padding: 8px 14px;
  width: fit-content;
}

.location-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background 0.3s;
}

.location-dot[data-state="loading"] {
  background: #fbbc04;
  animation: pulse 1.2s ease-in-out infinite;
}

.location-dot[data-state="ok"] {
  background: var(--color-success);
}

.location-dot[data-state="error"] {
  background: var(--color-error);
}

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

/* Origin Control */
.origin-control {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.origin-tabs,
.shape-tabs {
  display: flex;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  padding: 3px;
  width: fit-content;
  gap: 2px;
}

.origin-tab,
.shape-tab {
  padding: 6px 16px;
  font-size: 0.8125rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.origin-tab--active,
.shape-tab--active {
  background: var(--color-primary);
  color: #fff;
}

.origin-tab:not(.origin-tab--active):hover,
.shape-tab:not(.shape-tab--active):hover {
  background: rgba(66,133,244,.08);
  color: var(--color-text);
}

.origin-address-row {
  display: flex;
  gap: 8px;
}

.address-input {
  flex: 1;
  padding: 10px 14px;
  font-size: 0.9375rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.address-input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(66,133,244,.15);
}

.address-input::placeholder {
  color: var(--color-text-secondary);
}

.btn-address-search {
  padding: 10px 16px;
  font-size: 0.875rem;
  font-weight: 600;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-address-search:hover {
  background: #3367d6;
}

.address-status {
  font-size: 0.8125rem;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  margin-top: 2px;
}

.address-status[data-state="ok"] {
  color: var(--color-success);
  background: rgba(52,168,83,.08);
}

.address-status[data-state="error"] {
  color: var(--color-error);
  background: rgba(217,48,37,.08);
}

.address-status[data-state="loading"] {
  color: var(--color-text-secondary);
  background: var(--color-bg);
}

/* Shape Control */
.shape-control {
  display: flex;
  align-items: center;
  gap: 12px;
}

.shape-control-label {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text-secondary);
  white-space: nowrap;
}

/* Distance Control */
.distance-control {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 18px 16px 14px;
  box-shadow: var(--shadow-sm);
}

.distance-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--color-text);
  cursor: pointer;
}

.distance-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-primary);
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}

.distance-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: linear-gradient(
    to right,
    var(--color-primary) 0%,
    var(--color-primary) var(--slider-pct, 8.16%),
    var(--color-border) var(--slider-pct, 8.16%)
  );
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

.distance-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--color-primary);
  box-shadow: var(--shadow-sm);
  cursor: grab;
  transition: transform 0.1s, box-shadow 0.1s;
}

.distance-slider::-webkit-slider-thumb:active {
  transform: scale(1.15);
  cursor: grabbing;
  box-shadow: 0 0 0 6px rgba(66,133,244,.2);
}

.distance-slider::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: var(--color-primary);
  box-shadow: var(--shadow-sm);
  cursor: grab;
}

.distance-marks {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--color-text-secondary);
  padding: 0 2px;
}

/* Generate Button */
.btn-generate {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 16px;
  background: var(--color-primary);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-pill);
  cursor: pointer;
  box-shadow: var(--shadow-md);
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-generate:hover:not(:disabled) {
  background: #3367d6;
  box-shadow: var(--shadow-lg);
}

.btn-generate:active:not(:disabled) {
  transform: scale(0.97);
  box-shadow: var(--shadow-sm);
}

.btn-generate:disabled {
  background: var(--color-border);
  color: var(--color-text-secondary);
  box-shadow: none;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 0.875rem;
}

/* Spinner */
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

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

/* --- Map Section --- */
.map-section {
  margin: 16px 0 0;
}

.map-container {
  width: 100%;
  height: min(55vw, 340px);
  background: var(--color-border);
  position: relative;
}

/* --- Routes Section --- */
.routes-section {
  padding: 20px 16px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.routes-heading {
  font-size: 1.0625rem;
  font-weight: 700;
  color: var(--color-text);
}

.routes-subheading {
  font-size: 0.8125rem;
  color: var(--color-text-secondary);
  margin-top: -4px;
}

/* ルートリスト — 常に3列グリッド */
.routes-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

/* Route Card */
.route-card {
  background: var(--color-surface);
  border: 2px solid transparent;
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: border-color 0.2s, box-shadow 0.2s;
  cursor: pointer;
}

.route-card:hover {
  box-shadow: var(--shadow-md);
}

.route-card[data-selected="true"] {
  box-shadow: var(--shadow-md);
}

.route-card[data-route-index="0"] { --card-color: var(--route-1); }
.route-card[data-route-index="1"] { --card-color: var(--route-2); }
.route-card[data-route-index="2"] { --card-color: var(--route-3); }

.route-card[data-selected="true"] {
  border-color: var(--card-color);
}

.route-card-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.route-badge {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--card-color);
  color: #fff;
  font-size: 0.875rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.route-title {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--color-text);
  flex: 1;
}

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

.route-stat {
  background: var(--color-bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.route-stat-label {
  font-size: 0.6875rem;
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

.route-stat-value {
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--color-text);
  margin-top: 2px;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.btn-navigate {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 12px;
  background: var(--card-color);
  color: #fff;
  font-size: 0.9375rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-pill);
  cursor: pointer;
  text-decoration: none;
  transition: opacity 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.btn-navigate:hover {
  opacity: 0.9;
}

.btn-navigate:active {
  transform: scale(0.97);
}

.btn-navigate-icon {
  font-size: 1rem;
}

/* Re-generate Button */
.btn-regenerate {
  margin-top: 4px;
  padding: 14px;
  width: 100%;
  background: transparent;
  color: var(--color-primary);
  font-size: 0.9375rem;
  font-weight: 600;
  border: 2px solid var(--color-primary);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-regenerate:hover {
  background: rgba(66,133,244,.06);
}

.btn-regenerate:active {
  background: rgba(66,133,244,.12);
}

/* --- Error Toast --- */
.error-toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #323232;
  color: #fff;
  padding: 12px 20px;
  border-radius: var(--radius-pill);
  font-size: 0.875rem;
  font-weight: 500;
  max-width: calc(100vw - 32px);
  text-align: center;
  box-shadow: var(--shadow-lg);
  z-index: 200;
  animation: toast-in 0.25s ease;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(12px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* --- Utilities --- */
[hidden] { display: none !important; }

/* --- Mobile: コンパクトカードレイアウト --- */
@media (max-width: 639px) {
  .route-card {
    padding: 10px 8px;
    gap: 8px;
  }

  .route-badge {
    width: 26px;
    height: 26px;
    font-size: 0.75rem;
  }

  .route-title {
    font-size: 0.75rem;
  }

  /* statsを縦積みに */
  .route-stats {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .route-stat {
    padding: 6px 8px;
  }

  .route-stat-value {
    font-size: 0.9375rem;
  }

  /* ナビボタン: アイコンのみ */
  .btn-navigate-label {
    display: none;
  }

  .btn-navigate {
    padding: 8px 4px;
    font-size: 0.75rem;
  }
}

/* --- Desktop Layout --- */
@media (min-width: 640px) {
  .setup-section {
    padding: 28px 24px 12px;
  }

  .routes-section {
    padding: 24px 24px 0;
  }

  .map-container {
    height: 400px;
  }

  /* デスクトップは幅広3列 */
  .routes-list {
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 12px;
  }

  .route-card {
    padding: 16px;
    gap: 12px;
  }

  .route-badge {
    width: 32px;
    height: 32px;
    font-size: 0.875rem;
  }

  .route-title {
    font-size: 0.9375rem;
  }

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

  .route-stat {
    padding: 10px 12px;
  }

  .route-stat-value {
    font-size: 1.125rem;
  }

  .btn-navigate-label {
    display: inline;
  }

  .btn-navigate {
    padding: 12px;
    font-size: 0.9375rem;
  }
}
