/* One gap size for spacing between all controls and sections. */
:root {
  --gap: 10px;

  /* Light theme: white page and surfaces, faint gray top bar, light borders. */
  --bg: #fff;            /* page */
  --topbar: #fafafa;
  --topbar-button: #f0f0f0;
  --topbar-button-hover: #e6e6e6;
  --surface: #fff;       /* controls and menus */
  --panel: #fff;         /* timeline */
  --border: #d6d6d6;
  --grid: #ebebeb;       /* timeline lines */
  --hover: #f0f0f0;
  --text: #222;
  --muted: #777;         /* secondary text */
  --fill: #555;          /* checked checkbox */
  --error: #c00;
  --error-bg: #fdeeee;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1e1e;
    --topbar: #262626;
    --topbar-button: #2c2c2c;
    --topbar-button-hover: #3a3a3a;
    --surface: #2c2c2c;
    --panel: #262626;
    --border: #444;
    --grid: #383838;
    --hover: #3a3a3a;
    --text: #ddd;
    --muted: #888;
    --fill: #ddd;
    --error: #f66;
    --error-bg: #3a2222;
  }
}

/* Elements styled with display: flex would otherwise ignore the hidden attribute. */
[hidden] {
  display: none !important;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: var(--bg);
  color: var(--text);
  font-family: sans-serif;
  font-size: 14px;
}

.topbar {
  position: relative;
  display: flex;
  gap: calc(var(--gap) * 2);
  align-items: center;
  padding: var(--gap) 20px;
  background-color: var(--topbar);
  border-bottom: 1px solid var(--border);
}

.app-name {
  font-weight: bold;
  font-size: 16px;
}

.page {
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.dashboard-wrapper {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  width: 100%;
  max-width: 800px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap; /* controls go onto a second line on narrow screens */
  justify-content: space-between;
  align-items: center;
  gap: var(--gap);
}

.controls {
  display: flex;
  gap: var(--gap);
  align-items: center;
}

/* Below the timeline: Follow on the left, the range dropdown on the right. */
.timeline-controls {
  justify-content: space-between;
}

.tracker {
  display: flex;
  gap: var(--gap);
  flex: 1 1 100%; /* the full width of the page */
}

/* The main activity box and Start/Stop: larger than other controls. The # rules beat the
   general input and button rules. */
#activity,
#toggle {
  height: 40px;
  font-size: 18px;
}

#toggle {
  min-width: 100px;
}

.controls label {
  display: flex;
  align-items: center;
  gap: var(--gap);
  cursor: pointer;
  user-select: none;
}

input[type="checkbox"] {
  appearance: none;
  position: relative;
  width: 30px;
  height: 30px;
  background-color: var(--surface);
  border: 1px solid var(--border);
  color: var(--fill);
  cursor: pointer;
}

input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  inset: 6px;
  background-color: currentColor;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="time"],
button,
select {
  background-color: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  height: 30px; /* matches checkbox size */
  padding: 0 10px;
  font-family: sans-serif;
  font-size: 14px;
}

button,
select {
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.6;
}

a {
  color: inherit;
  text-decoration: none;
}

.error {
  color: var(--error);
}

.topbar-menu {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: calc(var(--gap) * 2);
}

.menu-toggle {
  display: none;
  margin-left: auto;
  width: 30px;
  padding: 0;
  align-items: center;
  justify-content: center;
}

/* Three lines: the middle one is the span, the others its ::before and ::after. */
.burger-icon,
.burger-icon::before,
.burger-icon::after {
  display: block;
  width: 16px;
  height: 2px;
  background-color: currentColor;
}

.burger-icon {
  position: relative;
}

.burger-icon::before,
.burger-icon::after {
  content: "";
  position: absolute;
  left: 0;
}

.burger-icon::before {
  top: -5px;
}

.burger-icon::after {
  top: 5px;
}

.topbar-group {
  display: flex;
  align-items: center;
  gap: var(--gap);
}

.nav {
  display: flex;
  gap: var(--gap);
}

.nav a[aria-current="page"] {
  font-weight: bold;
}

/* Top bar buttons are a shade darker than the bar, rather than white. */
.topbar button {
  background-color: var(--topbar-button);
}

.topbar button:hover {
  background-color: var(--topbar-button-hover);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  width: 100%;
  max-width: 300px;
}

/* The same size as the main activity box and Start/Stop. */
.auth-form input,
.auth-form button {
  height: 40px;
  font-size: 18px;
}

/* The "No account? / Have an account?" line under each form. */
.auth-switch {
  margin-top: var(--gap); /* on top of the form's gap */
  font-size: 16px;
  text-align: center;
}

.auth-switch a {
  font-weight: bold;
}

.auth-form input[aria-invalid="true"] {
  border-color: var(--error);
}

/* Right under its field: pulled up into half the form's gap. */
.field-error {
  margin-top: calc(var(--gap) / -2);
}

/* Errors about the whole form, like a wrong password, boxed above the button. */
.form-error {
  padding: 8px 10px;
  background-color: var(--error-bg);
  border: 1px solid var(--error);
}

/* Centred in the window. There's no top bar when logged out, so the page's padding is all
   that's above and below it. */
#auth {
  min-height: calc(100dvh - 40px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: var(--gap);
  width: 100%;
}

.activity-picker {
  position: relative;
  flex: 1;
  min-width: 0;
}

.activity-picker input {
  width: 100%;
}

.suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: -1px; /* overlap the input's bottom border */
  max-height: 200px;
  overflow-y: auto;
  list-style: none;
  background-color: var(--surface);
  border: 1px solid var(--border);
  z-index: 10;
}

.suggestions li {
  display: flex;
  align-items: center;
  height: 30px;
  padding: 0 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.suggestions li:hover,
.suggestions li.highlighted {
  background-color: var(--hover);
}

.suggestions li {
  gap: 8px;
}

.suggestion-swatch {
  flex: none;
  width: 12px;
  height: 12px;
}

/* In its own element so long names still end in "…" next to the swatch. */
.suggestion-label {
  overflow: hidden;
  text-overflow: ellipsis;
}

.suggestions li.selected {
  font-weight: bold;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.dropdown-labels {
  display: grid;
  text-align: left;
}

.dropdown-labels > span {
  grid-area: 1 / 1;
  visibility: hidden;
  white-space: nowrap;
}

.dropdown-labels > span.current {
  visibility: visible;
}

#timeline {
  position: relative;
  width: 100%;
  height: 127px; /* 33px above entry bars + bar height (80px) + 12px below + borders */
  overflow: hidden;
  background-color: var(--panel);
  border: 1px solid var(--border);
  touch-action: none;
  user-select: none;
}

#timeline {
  cursor: grab;
}

#timeline.dragging {
  cursor: grabbing;
}

/* Over an entry's edge, which can be dragged to change its start or end time. */
#timeline.over-edge {
  cursor: ew-resize;
}

.time-marker {
  position: absolute;
  top: 0;
  bottom: 0;
  border-left: 1px solid var(--grid);
  color: var(--muted);
  padding-left: 6px;
  padding-top: 6px;
  font-size: 13px;
  white-space: nowrap;
  pointer-events: none;
}

.timeline-wrap {
  position: relative;
}

/* Opens just below the timeline when an entry is clicked. */
.entry-editor {
  position: absolute;
  top: calc(100% + 6px);
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  width: 260px;
  padding: var(--gap);
  background-color: var(--surface);
  border: 1px solid var(--border);
}

.entry-editor-title {
  font-weight: bold;
}

.entry-editor label {
  display: grid;
  grid-template-columns: 70px 1fr;
  align-items: center;
}

.entry-editor .activity-picker {
  display: block;
}

/* Fill the column rather than using the browser's default input width, which can overflow. */
.entry-editor input {
  width: 100%;
  min-width: 0;
}

.entry-editor input:disabled {
  color: var(--muted);
}

.form-actions {
  display: flex;
  gap: var(--gap);
}

.now-line {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background-color: red;
  pointer-events: none;
}

.entry-bar {
  position: absolute; /* top and height are set in timeline.js */
  overflow: hidden;
  white-space: nowrap;
  transition: opacity 0.2s ease;
}

.entry-bar:hover {
  opacity: 0.8;
}

.entry-bar .label {
  padding: 5px 7px;
  font-size: 13px;
  color: #000;
  display: flex;
  flex-direction: column;
  gap: 3px;
  overflow: hidden;
}

.entry-bar .title {
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-bar .duration {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.8;
}

.chart-header {
  padding-bottom: var(--gap);
  border-bottom: 1px solid var(--border);
}

.section-title {
  font-weight: bold;
  font-size: 22px;
}

.back-link,
.muted {
  color: var(--muted);
}

/* A thin chevron drawn in CSS, pointing left, before the link text. */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  align-self: flex-start;
}

.back-link::before {
  content: "";
  width: 7px;
  height: 7px;
  border-left: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg);
}

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

.activity-header {
  display: flex;
  align-items: center;
  gap: var(--gap);
}

.color-picker {
  position: relative;
  display: flex;
}

.activity-color {
  width: 24px;
  height: 24px;
  padding: 0;
}

.palette {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(6, 24px);
  gap: 8px;
  padding: var(--gap);
  background-color: var(--surface);
  border: 1px solid var(--border);
}

.color-choice {
  width: 24px;
  height: 24px;
  padding: 0;
}

.color-choice[aria-pressed="true"] {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

/* The name reads as a heading, and becomes a box of the same size when clicked. */
.activity-name {
  height: auto;
  padding: 2px 6px;
  margin-left: -6px; /* line the text up with the edge despite the padding */
  background-color: transparent;
  border-color: transparent;
  cursor: text;
}

.activity-name:hover {
  background-color: var(--hover);
}

/* input.… so this beats the general input[type="text"] rule's font. */
input.activity-name-input {
  flex: 1;
  max-width: 400px;
  height: 36px;
  font-size: 22px;
  font-weight: bold;
}

.activity-history {
  display: flex;
  flex-direction: column;
  gap: calc(var(--gap) * 3);
  margin-top: calc(var(--gap) * 2);
}

.history-section {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--gap);
}

.subsection-title {
  font-size: 14px;
  font-weight: bold;
}

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

.entry-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--grid);
}

.entry-duration {
  text-align: right;
}

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

.chart-row {
  display: grid;
  grid-template-columns: minmax(60px, 150px) minmax(0, 1fr) auto;
  gap: var(--gap);
  align-items: center;
  height: 24px;
}

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

.chart-track {
  height: 100%;
}

.chart-bar {
  height: 100%;
  min-width: 2px;
}

.chart-duration {
  text-align: right;
  color: var(--muted);
}

.chart-empty {
  color: var(--muted);
}

/* Phones: less padding around the edges, and the top bar's links and account
   fold into a menu opened by the burger button. */
@media (max-width: 600px) {
  .topbar {
    padding: var(--gap) 12px;
  }

  .page {
    padding: 12px;
  }

  .menu-toggle {
    display: flex;
  }

  /* A full-width list: Track, History, Log out, with lines between. */
  .topbar-menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 20;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    background-color: var(--topbar);
    border-top: 1px solid var(--border); /* covers the top bar's own bottom line, so redraw it */
    border-bottom: 1px solid var(--border);
  }

  .topbar-menu.open {
    display: flex;
  }

  .nav,
  .topbar-group {
    flex-direction: column;
    align-items: stretch;
    gap: 0;
  }

  #username {
    display: none;
  }

  .nav a,
  .topbar-menu #logout {
    display: block;
    width: 100%;
    height: auto;
    padding: 8px 12px;
    border: none;
    border-bottom: 1px solid var(--border);
    background-color: transparent;
    color: inherit;
    font: inherit;
    text-align: left;
  }

  .topbar-menu #logout {
    border-bottom: none; /* the menu's own bottom border follows */
  }

  .nav a:hover,
  .topbar-menu #logout:hover {
    background-color: var(--topbar-button-hover);
  }
}
