:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-soft: #f0f4f2;
  --text: #1e2521;
  --muted: #6b756f;
  --line: #dce2dc;
  --green: #1f7a4f;
  --green-strong: #145f3b;
  --orange: #cf6f24;
  --blue: #2f6f9f;
  --red: #b34a3f;
  --shadow: 0 18px 45px rgba(37, 47, 42, 0.09);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.7), rgba(246, 245, 241, 0.92)),
    var(--bg);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang TC", "Microsoft JhengHei", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 24px;
}

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

.topbar h1,
.section-heading h2 {
  margin: 0;
  letter-spacing: 0;
}

.topbar h1 {
  font-size: clamp(28px, 5vw, 46px);
  line-height: 1.05;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--green);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.top-actions,
.button-row,
.input-row,
.section-heading {
  display: flex;
  align-items: center;
  gap: 12px;
}

.top-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  gap: 20px;
  align-items: start;
}

.panel,
.metric-card {
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.panel {
  padding: 18px;
  min-width: 0;
}

.import-panel {
  position: sticky;
  top: 20px;
  max-height: calc(100vh - 40px);
  overflow: auto;
}

.section-heading {
  justify-content: space-between;
  margin-bottom: 14px;
}

.section-heading h2 {
  font-size: 20px;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.input-row {
  align-items: stretch;
  margin-bottom: 14px;
}

.input-row label {
  flex: 1;
  min-width: 0;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  outline: none;
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease;
}

input {
  min-height: 42px;
  padding: 0 12px;
}

textarea {
  min-height: 230px;
  resize: vertical;
  padding: 12px;
  line-height: 1.58;
}

input:focus,
textarea:focus {
  border-color: rgba(31, 122, 79, 0.7);
  box-shadow: 0 0 0 3px rgba(31, 122, 79, 0.12);
}

.month-picker {
  grid-template-columns: auto minmax(150px, 1fr);
  align-items: center;
  padding: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.month-picker input {
  border: 0;
  min-height: 30px;
  padding: 0;
}

.button-row {
  margin-top: 14px;
  flex-wrap: wrap;
}

.primary-button,
.secondary-button,
.icon-button,
.danger-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 0 14px;
  font-weight: 800;
}

.primary-button {
  color: #fff;
  background: var(--green);
}

.primary-button:hover {
  background: var(--green-strong);
}

.secondary-button,
.icon-button {
  color: var(--text);
  background: var(--surface-soft);
  border-color: var(--line);
  text-decoration: none;
}

.secondary-button:hover,
.icon-button:hover {
  border-color: rgba(31, 122, 79, 0.45);
}

.danger-button {
  color: #7b2b25;
  background: rgba(179, 74, 63, 0.1);
  border-color: rgba(179, 74, 63, 0.2);
}

.danger-button:hover {
  border-color: rgba(179, 74, 63, 0.55);
}

.status-badge {
  white-space: nowrap;
  border-radius: 999px;
  padding: 6px 10px;
  color: var(--green-strong);
  background: rgba(31, 122, 79, 0.1);
  font-size: 12px;
  font-weight: 800;
}

.status-badge.local {
  color: var(--orange);
  background: rgba(207, 111, 36, 0.12);
}

.status-badge.error {
  color: var(--red);
  background: rgba(179, 74, 63, 0.12);
}

.status-badge.locked {
  color: var(--red);
  background: rgba(179, 74, 63, 0.12);
}

.login-panel,
.admin-panel {
  display: grid;
  gap: 14px;
}

.login-panel[hidden],
.admin-panel[hidden] {
  display: none;
}

.helper-text {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.manual-editor {
  display: grid;
  gap: 12px;
  margin-top: 4px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

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

.section-subheading h3 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0;
}

.compact-button {
  min-height: 34px;
  padding: 0 10px;
}

.admin-only {
  display: none;
}

.admin-authenticated .admin-only {
  display: table-cell;
}

.parse-preview {
  margin-top: 14px;
  display: grid;
  gap: 8px;
}

.preview-row,
.empty-state {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 36px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 13px;
}

.preview-row strong {
  color: var(--text);
}

.dashboard {
  display: grid;
  gap: 20px;
  min-width: 0;
}

.public-sheet-view .layout {
  grid-template-columns: minmax(0, 1fr);
}

.public-sheet-view .import-panel {
  display: none;
}

.public-sheet-view .app-shell {
  max-width: 1680px;
}

.hide-admin-link #publicViewLink {
  display: none;
}

.sheet-panel {
  min-width: 0;
  overflow: hidden;
  border: 1px solid #82aa68;
  border-radius: 8px;
  background: #eaf6dc;
  box-shadow: var(--shadow);
}

.sheet-titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid #82aa68;
  background: linear-gradient(180deg, #f8ffe9, #d9efc7);
}

.sheet-titlebar h2 {
  margin: 0;
  color: #b33131;
  font-size: 18px;
  letter-spacing: 0;
}

.sheet-label {
  margin: 0 0 4px;
  color: #2a6b25;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.sheet-meta {
  display: grid;
  gap: 3px;
  min-width: 180px;
  text-align: right;
}

.sheet-meta strong {
  color: #2b5f25;
  font-size: 14px;
}

.sheet-meta span {
  color: #5a6b4c;
  font-size: 12px;
  font-weight: 800;
}

.sheet-scroller {
  max-width: 100%;
  overflow: auto;
  background: #dcedcd;
}

.monthly-sheet {
  width: max-content;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  color: #1a2519;
  font-size: 12px;
  line-height: 1.2;
}

.monthly-sheet th,
.monthly-sheet td {
  height: 28px;
  min-width: 34px;
  border-right: 1px solid #8bb978;
  border-bottom: 1px solid #8bb978;
  padding: 4px 5px;
  text-align: center;
  vertical-align: middle;
}

.monthly-sheet th {
  position: sticky;
  top: 0;
  z-index: 4;
  color: #173a16;
  background: #7fc34d;
  font-weight: 900;
  text-transform: none;
}

.monthly-sheet tbody td {
  background: #e9f6df;
}

.monthly-sheet tbody tr:nth-child(even) td {
  background: #dff0d2;
}

.monthly-sheet tfoot td {
  position: sticky;
  bottom: 0;
  z-index: 3;
  background: #b8dda4;
  font-weight: 900;
}

.monthly-sheet .sheet-rank-col,
.monthly-sheet .sheet-name-col,
.monthly-sheet .sheet-total-col,
.monthly-sheet .sheet-days-col {
  position: sticky;
  z-index: 2;
  font-weight: 800;
}

.monthly-sheet th.sheet-rank-col,
.monthly-sheet th.sheet-name-col,
.monthly-sheet th.sheet-total-col,
.monthly-sheet th.sheet-days-col {
  z-index: 5;
}

.monthly-sheet .sheet-rank-col {
  left: 0;
  min-width: 42px;
  background: #c8e8b8;
}

.monthly-sheet .sheet-name-col {
  left: 42px;
  min-width: 88px;
  text-align: left;
  background: #d8e9f8;
}

.monthly-sheet .sheet-total-col {
  left: 130px;
  min-width: 68px;
  background: #eef7ff;
}

.monthly-sheet .sheet-days-col {
  left: 198px;
  min-width: 48px;
  background: #edf7fb;
}

.monthly-sheet th.sheet-rank-col,
.monthly-sheet th.sheet-name-col,
.monthly-sheet th.sheet-total-col,
.monthly-sheet th.sheet-days-col {
  background: #7fc34d;
}

.monthly-sheet .sheet-day-cell.has-run {
  background: #ffe300;
  color: #675000;
  font-weight: 900;
}

.monthly-sheet .sheet-day-cell.has-total {
  background: #ffd65a;
  color: #4c3b00;
}

.monthly-sheet .is-selected-day {
  box-shadow: inset 0 0 0 2px #d92727;
}

.monthly-sheet .sheet-empty {
  padding: 24px;
  color: #617552;
  font-weight: 800;
  text-align: center;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.metric-card {
  min-height: 104px;
  padding: 16px;
}

.metric-card span {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.metric-card strong {
  display: block;
  margin-top: 12px;
  font-size: clamp(24px, 3vw, 34px);
  line-height: 1;
}

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

.rank-row {
  display: grid;
  grid-template-columns: 44px minmax(120px, 1fr) minmax(90px, auto);
  align-items: center;
  gap: 12px;
  min-height: 54px;
}

.rank-number {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: #fff;
  background: var(--green);
  font-weight: 900;
}

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

.rank-name {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 7px;
}

.rank-name strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-name span,
.muted {
  color: var(--muted);
  font-size: 13px;
}

.bar-track {
  width: 100%;
  height: 9px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8ece8;
}

.bar-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--green), var(--blue));
}

.rank-distance {
  font-weight: 900;
  text-align: right;
}

.split-section {
  display: grid;
  grid-template-columns: 1.15fr 0.85fr;
  gap: 20px;
}

.trend-chart {
  display: flex;
  align-items: end;
  gap: 7px;
  min-height: 230px;
  padding-top: 10px;
}

.day-bar {
  position: relative;
  flex: 1;
  min-width: 0;
  height: 190px;
  display: flex;
  align-items: end;
}

.day-bar span {
  display: block;
  width: 100%;
  min-height: 3px;
  border-radius: 6px 6px 0 0;
  background: var(--blue);
}

.day-bar[data-label]::after {
  content: attr(data-label);
  position: absolute;
  left: 50%;
  bottom: -24px;
  transform: translateX(-50%);
  color: var(--muted);
  font-size: 11px;
}

.today-list {
  display: grid;
  gap: 8px;
}

.today-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 42px;
  padding: 9px 10px;
  border-radius: 8px;
  background: var(--surface-soft);
}

.today-row strong,
.today-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search-input {
  max-width: 220px;
}

.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
}

table {
  width: 100%;
  min-width: 620px;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 12px 10px;
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

td {
  font-size: 14px;
}

.source-cell {
  color: var(--muted);
  max-width: 420px;
}

.action-cell {
  min-width: 124px;
  white-space: nowrap;
}

.table-action {
  min-height: 30px;
  margin-right: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--text);
  font-size: 12px;
  font-weight: 800;
}

.table-action:hover {
  border-color: rgba(31, 122, 79, 0.45);
}

.table-action.danger {
  color: #7b2b25;
  background: rgba(179, 74, 63, 0.1);
  border-color: rgba(179, 74, 63, 0.2);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  max-width: min(520px, calc(100vw - 32px));
  padding: 12px 14px;
  border-radius: 8px;
  color: #fff;
  background: #24302a;
  box-shadow: var(--shadow);
  transition:
    opacity 0.18s ease,
    transform 0.18s ease;
}

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

@media (max-width: 1080px) {
  .layout,
  .split-section {
    grid-template-columns: minmax(0, 1fr);
  }

  .import-panel {
    position: static;
  }

  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 680px) {
  .app-shell {
    padding: 16px;
  }

  .topbar,
  .section-heading,
  .input-row {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions,
  .button-row {
    justify-content: stretch;
  }

  .top-actions > *,
  .button-row > *,
  .search-input {
    width: 100%;
    max-width: none;
  }

  .metric-grid {
    grid-template-columns: 1fr;
  }

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

  .rank-distance {
    grid-column: 2;
    text-align: left;
  }

  .trend-chart {
    gap: 3px;
  }

  .sheet-titlebar {
    align-items: stretch;
    flex-direction: column;
  }

  .sheet-meta {
    min-width: 0;
    text-align: left;
  }

  .monthly-sheet {
    font-size: 11px;
  }

  .monthly-sheet th,
  .monthly-sheet td {
    min-width: 30px;
    height: 26px;
    padding: 3px 4px;
  }

  .monthly-sheet .sheet-rank-col {
    min-width: 36px;
  }

  .monthly-sheet .sheet-name-col {
    left: 36px;
    min-width: 76px;
  }

  .monthly-sheet .sheet-total-col {
    left: 112px;
    min-width: 58px;
  }

  .monthly-sheet .sheet-days-col {
    left: 170px;
    min-width: 42px;
  }
}
