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

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font-family);
  font-size: var(--font-size-md);
  line-height: var(--line-height-body);
  color: var(--text-primary);
  background: var(--surface-app);
}

body {
  min-height: 100vh;
}

a {
  color: var(--text-link);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

a:hover {
  text-decoration-thickness: 2px;
}

:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.skip-link {
  position: absolute;
  left: var(--space-3);
  top: -100px;
  z-index: 1000;
  background: var(--surface-panel);
  color: var(--text-primary);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
}

.skip-link:focus {
  top: var(--space-3);
}

.app-shell {
  display: grid;
  grid-template-columns: var(--nav-width) 1fr;
  min-height: 100vh;
}

.nav-rail {
  background: var(--surface-rail);
  color: var(--text-inverse);
  display: flex;
  flex-direction: column;
  padding: var(--space-4) var(--space-3);
  gap: var(--space-5);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2);
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: var(--radius-sm);
  background: #2f81ed;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-bold);
}

.brand-name {
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-semibold);
  letter-spacing: 0.01em;
}

.nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.nav-link {
  display: block;
  color: #d7dee8;
  text-decoration: none;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius-sm);
  border-left: 3px solid transparent;
  min-height: 44px;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
}

.nav-link[aria-current="page"] {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border-left-color: #7db4ff;
  font-weight: var(--font-weight-semibold);
}

.nav-footer {
  margin-top: auto;
  display: grid;
  gap: var(--space-2);
  padding: var(--space-2);
}

.sync-pill,
.mode-pill {
  margin: 0;
  font-size: var(--font-size-xs);
  color: #c5ced9;
}

.mode-pill {
  color: #9fd4b0;
}

.workspace {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-6) var(--space-4);
  background: var(--surface-panel);
  border-bottom: 1px solid var(--border-subtle);
}

.page-header-text {
  display: grid;
  gap: var(--space-1);
}

.page-header h1 {
  margin: 0;
  font-size: var(--font-size-xl);
  line-height: var(--line-height-tight);
  font-weight: var(--font-weight-semibold);
}

.page-subtitle {
  margin: 0;
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
}

.back-btn {
  justify-self: start;
  appearance: none;
  border: 1px solid var(--border-subtle);
  background: var(--surface-panel);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  min-height: 32px;
  padding: 0 var(--space-3);
  cursor: pointer;
  margin-bottom: var(--space-2);
}

.back-btn[hidden] {
  display: none !important;
}

.command-bar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
  min-height: var(--command-bar-height);
}

.main {
  padding: var(--space-5) var(--space-6) var(--space-8);
}

.status-region:empty {
  display: none;
}

.status-region:not(:empty) {
  margin-bottom: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--semantic-open-bg);
  border: 1px solid #bfd5ef;
  color: var(--text-primary);
}

.status-region[data-tone="error"] {
  background: var(--semantic-overdue-bg);
  border-color: #f0b4ae;
}

.status-region[data-tone="success"] {
  background: var(--semantic-won-bg);
  border-color: #b7dfc5;
}

.panel {
  background: var(--surface-panel);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: var(--elevation-1);
}

.panel + .panel {
  margin-top: var(--space-4);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
}

.panel-header h2 {
  margin: 0;
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-semibold);
}

.panel-body {
  padding: var(--space-4);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  align-items: end;
  margin-bottom: var(--space-4);
}

.field {
  display: grid;
  gap: var(--space-1);
}

.field label {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.field input,
.field select,
.btn {
  min-height: var(--control-height);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  font: inherit;
}

.field input,
.field select {
  padding: 0 var(--space-3);
  background: var(--surface-panel);
  color: var(--text-primary);
  min-width: 16rem;
}

.btn {
  appearance: none;
  background: var(--surface-panel);
  color: var(--text-primary);
  padding: 0 var(--space-4);
  cursor: pointer;
}

.btn:hover {
  background: var(--surface-hover);
}

.btn-primary {
  background: var(--semantic-info);
  border-color: var(--semantic-info);
  color: #fff;
}

.btn-primary:hover {
  filter: brightness(0.95);
  background: var(--semantic-info);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  margin-bottom: var(--space-3);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th,
.data-table td {
  text-align: left;
  padding: 0.7rem 0.75rem;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: top;
}

.data-table th {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  font-weight: var(--font-weight-semibold);
  background: var(--surface-muted);
}

.data-table tbody tr:hover {
  background: var(--surface-hover);
}

.data-table a {
  font-weight: var(--font-weight-semibold);
  text-decoration: none;
}

.data-table a:hover,
.data-table a:focus-visible {
  text-decoration: underline;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 1.5rem;
  padding: 0 0.5rem;
  border-radius: 999px;
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  background: var(--surface-muted);
  color: var(--text-secondary);
}

.badge-open {
  background: var(--semantic-open-bg);
  color: var(--semantic-info);
}

.badge-won,
.badge-success {
  background: var(--semantic-won-bg);
  color: var(--semantic-success);
}

.badge-overdue,
.badge-danger {
  background: var(--semantic-overdue-bg);
  color: var(--semantic-danger);
}

.badge-warning {
  background: #fff4d6;
  color: var(--semantic-warning);
}

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

.stat-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.stat {
  background: var(--surface-panel);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-4);
}

.stat a {
  text-decoration: none;
  color: inherit;
  display: grid;
  gap: var(--space-1);
}

.stat .label {
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
}

.stat .value {
  font-size: 1.75rem;
  font-weight: var(--font-weight-semibold);
  line-height: 1;
}

.col-7 {
  grid-column: span 7;
}

.col-5 {
  grid-column: span 5;
}

.detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  gap: var(--space-4);
}

.kv {
  display: grid;
  grid-template-columns: 10rem 1fr;
  gap: var(--space-2) var(--space-3);
}

.kv dt {
  margin: 0;
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
}

.kv dd {
  margin: 0;
}

.empty,
.loading,
.error-state {
  padding: var(--space-6);
  text-align: left;
  color: var(--text-secondary);
}

.error-state {
  color: var(--semantic-danger);
}

.list-plain {
  list-style: none;
  margin: 0;
  padding: 0;
}

.list-plain li {
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--border-subtle);
}

.list-plain li:last-child {
  border-bottom: 0;
}

.list-plain a {
  font-weight: var(--font-weight-semibold);
  text-decoration: none;
}

.muted {
  color: var(--text-muted);
}

@media (max-width: 1100px) {
  .stat-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .col-7,
  .col-5 {
    grid-column: span 12;
  }

  .detail-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .nav-rail {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
  }

  .nav-list {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .nav-footer {
    width: 100%;
    margin-top: 0;
  }

  .page-header,
  .main {
    padding-left: var(--space-4);
    padding-right: var(--space-4);
  }

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

  .field input,
  .field select {
    min-width: 12rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
