/* ==========================================================================
   Inkpace — Nord Minimalist Typing Workspace
   ========================================================================== */

:root {
  --bg: #2E3440;
  --hairline: #343B49;
  --hairline-strong: #464F61;

  --text: #ECEFF4;
  --text-secondary: #98A3B8;
  --text-untyped: #A6B1C4;

  --accent: #88C0D0;
  --accent-soft: rgba(136, 192, 208, 0.12);
  --accent-t1: rgba(136, 192, 208, 0.28);
  --accent-t2: rgba(136, 192, 208, 0.6);
  --accent-t3: #88C0D0;

  --word-active: rgba(236, 239, 244, 0.055);
  --error: #BF616A;
  --warning: #EBCB8B;
  --cal-0: #313744;

  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  min-height: 100vh;
  background-color: #2E3440;
  overflow-x: hidden;
}

body {
  background-color: #2E3440;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button,
textarea {
  font-family: inherit;
}

.tabular {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.hidden {
  display: none !important;
}

button:focus-visible {
  outline: 1.5px solid var(--accent);
  outline-offset: 3px;
  border-radius: 3px;
}

/* -------------------- Shell & Topbar -------------------- */
.shell {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 0 28px 64px;
}

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  padding: 24px 0 16px;
  border-bottom: 1px solid var(--hairline);
  transition: opacity 260ms ease;
}

.topbar-left {
  display: flex;
  align-items: baseline;
  gap: 28px;
}

.wordmark {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1;
}

.wordmark-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.top-nav {
  display: inline-flex;
  align-items: baseline;
  gap: 18px;
}

.nav-link {
  position: relative;
  background: none;
  border: none;
  padding: 0 0 3px;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 130ms ease;
}

.nav-link:hover {
  color: var(--text);
}

.nav-link.is-active {
  color: var(--text);
  font-weight: 600;
}

.nav-link.is-active::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 1.5px;
  background-color: var(--accent);
}

.topbar-right {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.text-toggle {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 130ms ease;
}

.text-toggle:hover {
  color: var(--text);
}

.text-toggle[aria-pressed="true"] {
  color: var(--accent);
  font-weight: 600;
}

/* -------------------- Focus Dimming While Typing -------------------- */
body.is-typing .topbar,
body.is-typing .controls-bar,
body.is-typing .insights-grid,
body.is-typing .test-actions {
  opacity: 0.25;
}

body.is-typing .topbar:hover,
body.is-typing .topbar:focus-within,
body.is-typing .controls-bar:hover,
body.is-typing .controls-bar:focus-within,
body.is-typing .insights-grid:hover,
body.is-typing .insights-grid:focus-within,
body.is-typing .test-actions:hover,
body.is-typing .test-actions:focus-within {
  opacity: 1;
}

/* -------------------- Views & Practice Layout -------------------- */
.view {
  opacity: 1;
  transition: opacity 150ms ease;
}

.view.is-fading {
  opacity: 0;
}

.practice-main {
  padding-top: 48px;
}

/* -------------------- Plain Text Controls (Length & Difficulty) -------------------- */
.controls-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px 20px;
  margin-bottom: 32px;
  transition: opacity 260ms ease;
}

.control-group {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 14px;
}

.control-sep {
  color: var(--hairline-strong);
  font-size: 0.9rem;
  user-select: none;
}

.opt-btn {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.88rem;
  font-weight: 400;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 120ms ease;
}

.opt-btn:hover {
  color: var(--text);
}

.opt-btn.is-active {
  color: var(--accent);
  font-weight: 600;
}

.weak-mode-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 10px;
  font-size: 0.86rem;
  font-weight: 400;
  color: var(--text-secondary);
}

.weak-stop-link {
  border: none;
  background: none;
  padding: 0;
  color: var(--accent);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}

.weak-stop-link:hover {
  text-decoration: underline;
}

/* -------------------- Open Typing Area -------------------- */
.stage-section {
  width: 100%;
}

.stage-readout {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 8px;
}

.readout-left {
  display: inline-flex;
  align-items: baseline;
  gap: 18px;
}

.readout-timer {
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--text);
}

.readout-stat {
  font-size: 0.88rem;
  color: var(--text-secondary);
}

.readout-badge {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--warning);
}

.readout-passage {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.progress-hairline {
  width: 100%;
  height: 1px;
  background-color: var(--hairline);
  margin-bottom: 0;
}

.typing-stage {
  position: relative;
  cursor: text;
}

.paragraph-box {
  position: relative;
  font-family: var(--font-mono);
  font-size: 32px;
  line-height: 1.95;
  height: calc(3 * 1.95em + 23px);
  overflow: hidden;
}

.paragraph-display {
  position: relative;
  padding: 23px 0 0;
  color: var(--text-untyped);
  user-select: none;
}

.word {
  display: inline-block;
  white-space: nowrap;
  border-radius: 4px;
}

.word.is-active-word .char:not(.char-space) {
  background-color: var(--word-active);
}

.word.is-active-word .char:not(.char-space):first-child {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}

.word.is-active-word .char:not(.char-space):has(+ .char-space),
.word.is-active-word .char:not(.char-space):last-child {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}

.char {
  position: relative;
  display: inline-block;
  white-space: pre;
  color: var(--text-untyped);
  vertical-align: baseline;
  transition: color 45ms linear;
}

.char.is-correct {
  color: var(--text);
}

.char.is-incorrect {
  color: var(--text);
}

.char.is-incorrect::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 4px;
  height: 2px;
  background-color: var(--error);
  pointer-events: none;
}

.char.is-incorrect::before {
  content: attr(data-expected);
  position: absolute;
  left: 0;
  right: 0;
  top: -13px;
  font-size: 12px;
  font-weight: 500;
  line-height: 1;
  text-align: center;
  color: var(--text-secondary);
  opacity: 0.9;
  pointer-events: none;
}

.caret {
  position: absolute;
  top: 0;
  left: 0;
  width: 2.5px;
  border-radius: 1px;
  background-color: var(--accent);
  pointer-events: none;
  transition: transform 85ms cubic-bezier(0.22, 1, 0.36, 1);
}

.caret.no-transition {
  transition: none;
}

.caret.is-blinking {
  animation: caretBlink 1.05s ease-in-out infinite;
}

@keyframes caretBlink {
  0%, 45% { opacity: 1; }
  55%, 100% { opacity: 0.15; }
}

.focus-cue {
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--text-secondary);
  pointer-events: none;
}

.typing-input {
  position: absolute;
  top: 23px;
  left: 0;
  width: 1px;
  height: 1px;
  font-size: 16px;
  opacity: 0;
  border: 0;
  padding: 0;
  margin: 0;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  pointer-events: none;
  overflow: hidden;
}

/* -------------------- Test Actions -------------------- */
.test-actions {
  margin-top: 24px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  transition: opacity 260ms ease;
}

.action-buttons {
  display: inline-flex;
  align-items: baseline;
  gap: 18px;
}

.action-btn {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 130ms ease;
}

.action-btn:hover {
  color: var(--text);
}

.shortcut-note {
  font-size: 0.78rem;
  color: var(--text-secondary);
}

/* -------------------- Results -------------------- */
.results-block {
  padding-top: 6px;
}

.results-header {
  display: flex;
  align-items: baseline;
  gap: 14px;
  margin-bottom: 20px;
}

.results-session-meta {
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.pb-badge {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--accent);
}

.results-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 24px 48px;
}

.result-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.result-wpm-row {
  display: inline-flex;
  align-items: baseline;
  gap: 12px;
}

.result-peak {
  font-size: 0.84rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.result-num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: clamp(2.25rem, 4.5vw, 3.1rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.03em;
  color: var(--text);
}

.result-num.is-accent {
  color: var(--accent);
}

.result-label {
  font-size: 0.82rem;
  color: var(--text-secondary);
}

.session-chart-box {
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--hairline);
}

.session-chart-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.session-chart-svg {
  display: block;
  width: 100%;
  height: 76px;
}

.results-diff {
  margin: 22px 0 0;
  font-size: 0.88rem;
  color: var(--text-secondary);
}

.results-actions {
  margin-top: 22px;
  display: flex;
  align-items: baseline;
  gap: 22px;
}

.result-link {
  position: relative;
  background: none;
  border: none;
  padding: 0 0 3px;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
}

.result-link:focus-visible {
  outline: none;
}

.result-link::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 1px;
  background-color: var(--hairline-strong);
}

.result-link:hover::after,
.result-link:focus-visible::after {
  background-color: var(--text);
}

/* -------------------- Below-Stage Log -------------------- */
.insights-grid {
  margin-top: 64px;
  padding-top: 24px;
  border-top: 1px solid var(--hairline);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 40px;
  transition: opacity 260ms ease;
}

.insight-col {
  display: flex;
  flex-direction: column;
}

.panel-head-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.weak-head {
  display: flex;
  align-items: baseline;
  justify-content: flex-start;
  gap: 16px;
  margin-bottom: 14px;
}

.panel-heading {
  margin: 0;
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--text);
}

.panel-count {
  font-size: 0.76rem;
  color: var(--text-secondary);
}

.today-empty,
.weak-empty {
  margin: 0;
  font-size: 0.83rem;
  color: var(--text-secondary);
}

.today-list {
  list-style: none;
  margin: 0 0 14px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 7px;
  max-height: 150px;
  overflow-y: auto;
}

.today-row {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 12px;
  font-size: 0.81rem;
  align-items: baseline;
}

.today-time {
  color: var(--text-secondary);
}

.today-mode {
  font-size: 0.76rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.today-wpm {
  color: var(--text);
  font-weight: 600;
  text-align: right;
}

.today-acc {
  color: var(--text-secondary);
  text-align: right;
  min-width: 42px;
}

.today-summary {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--hairline);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.today-summary-stats {
  display: flex;
  align-items: baseline;
  gap: 18px;
}

.summary-pair {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.summary-label {
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.summary-value {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--text);
}

.sparkline-svg {
  display: block;
  width: 110px;
  height: 26px;
  flex-shrink: 0;
}

.practice-link {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--accent);
  cursor: pointer;
}

.practice-link:hover {
  text-decoration: underline;
}

.weak-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

.weak-row {
  display: flex;
  align-items: baseline;
  justify-content: flex-start;
  gap: 24px;
  font-size: 0.84rem;
}

.weak-key {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--text);
  width: 1ch;
}

.weak-count {
  color: var(--text-secondary);
  font-size: 0.8rem;
}

/* -------------------- History View -------------------- */
.history-view {
  padding-top: 32px;
  display: flex;
  flex-direction: column;
  gap: 36px;
}

.history-summary-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20px 44px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--hairline);
}

.history-stat-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

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

.history-stat-value {
  font-size: 1.55rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
}

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

.calendar-header,
.sessions-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.history-section-title {
  margin: 0;
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text);
}

.calendar-outer {
  display: flex;
  align-items: stretch;
  gap: 10px;
  width: 100%;
  padding-bottom: 4px;
}

.calendar-weekdays {
  display: grid;
  grid-template-rows: repeat(7, 20px);
  gap: 4px;
  padding-top: 20px;
  font-size: 0.68rem;
  color: var(--text-secondary);
  line-height: 20px;
  flex-shrink: 0;
}

.calendar-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.calendar-months {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 4px;
  font-size: 0.68rem;
  color: var(--text-secondary);
  height: 14px;
  line-height: 14px;
}

.cal-month-label {
  white-space: nowrap;
}

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

.cal-week {
  display: grid;
  grid-template-rows: repeat(7, 20px);
  gap: 4px;
}

.cal-day {
  width: 100%;
  height: 20px;
  border-radius: 3px;
  background-color: var(--cal-0);
}

.cal-day.is-future {
  background-color: transparent;
  border: 1px solid var(--hairline);
}

.cal-day.t1 { background-color: var(--accent-t1); }
.cal-day.t2 { background-color: var(--accent-t2); }
.cal-day.t3 { background-color: var(--accent-t3); }

.calendar-legend {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.74rem;
  color: var(--text-secondary);
}

.legend-swatch {
  width: 11px;
  height: 11px;
  border-radius: 2px;
  display: inline-block;
}

.clear-history-wrap {
  display: flex;
  align-items: baseline;
}

.clear-confirm-group {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

.confirm-prompt,
.confirm-sep {
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.plain-danger,
.plain-action {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
}

.plain-danger:hover,
.plain-danger.is-strong {
  color: var(--error);
}

.plain-action:hover {
  color: var(--text);
}

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

.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.history-table th,
.history-table td {
  text-align: left;
  padding: 11px 14px 11px 0;
  border-bottom: 1px solid var(--hairline);
}

.history-table th {
  font-weight: 500;
  color: var(--text-secondary);
}

.history-table th.col-action,
.history-table td.col-action {
  text-align: right;
  padding-right: 0;
  width: 28px;
}

.delete-row-btn {
  background: none;
  border: none;
  padding: 0 4px;
  color: var(--text-secondary);
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0.55;
}

.delete-row-btn:hover {
  opacity: 1;
  color: var(--error);
}

.sort-btn {
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.sort-btn:hover,
.sort-btn.is-sorted {
  color: var(--text);
  font-weight: 700;
}

.history-empty {
  margin: 0;
  font-size: 0.86rem;
  color: var(--text-secondary);
}

/* -------------------- Responsive -------------------- */
@media (max-width: 780px) {
  .shell {
    padding: 0 18px 36px;
  }

  .insights-grid {
    grid-template-columns: 1fr;
    gap: 28px;
  }
}

@media (max-width: 600px) {
  .shell {
    width: 100%;
    padding: 0 16px 40px;
  }

  .topbar {
    flex-wrap: wrap;
    padding: 18px 0 14px;
  }

  .topbar-left {
    gap: 16px;
  }

  .controls-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .controls-bar > .control-sep,
  .weak-mode-chip > .control-sep {
    display: none;
  }

  .paragraph-box {
    font-size: 22px;
    line-height: 1.95;
    height: calc(3 * 1.95em + 23px);
  }

  .char.is-incorrect::before {
    top: -11px;
    font-size: 10px;
  }

  .shortcut-note {
    display: none;
  }

  .results-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 20px 24px;
  }

  .session-chart-box,
  .session-chart-svg {
    width: 100%;
    max-width: 100%;
  }

  .history-summary-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 20px 24px;
  }

  .calendar-outer {
    gap: 8px;
  }

  .table-wrap {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .history-table {
    min-width: 480px;
  }
}
