/* ═══════════════════════════════════════════════════
   BASE
   ═══════════════════════════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Rethink Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.3s, color 0.3s;
  overflow: hidden;
  /* Safe area padding for iPhones with notch/dynamic island */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
button { cursor: pointer; border: none; background: none; font-family: inherit; color: inherit; }
input { font-family: inherit; color: inherit; }

/* ═══════════════════════════════════════════════════
   APP LAYOUT
   ═══════════════════════════════════════════════════ */
.app {
  width: 100%;
  height: 100vh;           /* fallback for older browsers */
  height: 100dvh;          /* dynamic viewport height — adapts to Safari toolbar */
  display: flex;
  flex-direction: column;
  background: var(--bg);
  position: relative;
  z-index: 2;              /* above bonsai SVG overlay (z-index: 1) */
}

/* ═══════════════════════════════════════════════════
   HEADER
   ═══════════════════════════════════════════════════ */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-title {
  font-family: 'Abhaya Libre', serif;
  font-weight: 600;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 6px;
  letter-spacing: 0.01em;
}

.header-logo {
  width: 20px;
  height: 20px;
  object-fit: contain;
  transition: filter 0.3s;
}
[data-theme="dark"] .header-logo {
  filter: invert(1) brightness(1.2);
}

.header-actions { display: flex; gap: 2px; }
.header-btn {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  transition: background 0.15s;
}
.header-btn:hover, .header-btn:active { background: var(--hover-bg); }
.header-btn.muted { opacity: 0.5; }

/* ═══════════════════════════════════════════════════
   MAIN
   ═══════════════════════════════════════════════════ */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  position: relative;
}

/* Holds the timer (and future side panels). flex-row ready for a coworking panel. */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
  order: 1;
}

/* ═══════════════════════════════════════════════════
   SESSION PANEL (bottom — segments list & toolbar)
   ═══════════════════════════════════════════════════ */
.session-panel {
  flex: 0 0 auto;
  order: 2;
  position: relative;
  overflow: visible;
}

.session-panel-inner {
  max-height: 200px;
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  transition: max-height 0.25s ease, opacity 0.2s ease;
  overflow: hidden;
}

.session-panel.collapsed .session-panel-inner {
  max-height: 0;
  border-top: none;
  opacity: 0;
  pointer-events: none;
}

.session-panel.collapsed .panel-collapse-tab {
  opacity: 0;
  pointer-events: none;
}

/* Tab-style expand/collapse button */
.panel-tab-btn {
  position: absolute;
  left: 12px;
  height: 22px;
  padding: 0 12px;
  border-radius: 8px 8px 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: var(--border);
  border: 1px solid var(--border);
  border-bottom: none;
  z-index: 10;
  cursor: pointer;
  transition: background 0.15s, opacity 0.2s;
}
.tab-label {
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  transition: color 0.15s;
}
.panel-tab-btn:hover .tab-label,
.panel-tab-btn:active .tab-label { color: white; }
.panel-tab-btn:hover, .panel-tab-btn:active { background: var(--accent); }
.panel-tab-btn:hover .tab-arrow, .panel-tab-btn:active .tab-arrow { border-color: white; }
.tab-arrow {
  width: 8px;
  height: 8px;
  border-left: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transition: border-color 0.15s;
}
.tab-arrow.down { transform: rotate(-45deg) translateY(-2px); }
.tab-arrow.up { transform: rotate(135deg) translateY(2px); }

/* Expand tab — anchored at bottom of timer area, above safe area on iPhones */
.session-expand-btn {
  bottom: 8px;
  bottom: calc(8px + env(safe-area-inset-bottom));
  opacity: 0;
  pointer-events: none;
}
.session-expand-btn.visible {
  opacity: 1;
  pointer-events: auto;
}

/* Collapse tab — sits above the panel border */
.panel-collapse-tab {
  top: -22px;
  padding: 0 10px;
}
.panel-collapse-tab .tab-label { display: none; }

/* Hide "Segments" label on very narrow screens */
@media (max-width: 324px) {
  .session-expand-btn .tab-label { display: none; }
}

/* Panel header */
.session-header {
  padding: 4px 8px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: opacity 0.15s;
}

.session-name {
  font-family: 'Abhaya Libre', serif;
  font-weight: 600;
  font-size: 13px;
  line-height: 1.3;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.session-name-input {
  font-family: 'Abhaya Libre', serif;
  font-weight: 600;
  font-size: 13px;
  flex: 1;
  min-width: 0;
  padding: 1px 4px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  background: var(--modal-bg);
  color: var(--text);
  outline: none;
}

.session-toolbar {
  display: flex;
  gap: 1px;
  align-items: center;
  flex-shrink: 0;
}

.session-btn {
  width: 28px;
  height: 26px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  transition: background 0.15s;
  flex-shrink: 0;
}
.session-btn:hover, .session-btn:active { background: var(--hover-bg); }

/* Segment list */
.session-segments {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.segment-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.1s;
  white-space: nowrap;
}
.segment-item:hover, .segment-item:active { background: var(--hover-bg); }
.segment-item.active { background: var(--active-bg); font-weight: 500; }

/* Drag handle */
.segment-drag-handle {
  cursor: grab;
  color: var(--text-muted);
  font-size: 10px;
  flex-shrink: 0;
  width: 14px;
  text-align: center;
  user-select: none;
  opacity: 0;
  transition: opacity 0.15s;
  letter-spacing: -2px;
}
.segment-item:hover .segment-drag-handle { opacity: 0.6; }
.segment-drag-handle:hover { opacity: 1 !important; }
.segment-drag-handle:active { cursor: grabbing; }
.segment-drag-handle.disabled { opacity: 0.15 !important; pointer-events: none; cursor: default; }

.segment-num {
  font-family: 'DM Mono', monospace;
  font-size: 10px;
  color: var(--text-muted);
  width: 14px;
  text-align: center;
  flex-shrink: 0;
}

.segment-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
}

.segment-icon.completed { color: var(--completed); }
.segment-icon.playing { color: var(--accent); }

.segment-title-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.segment-duration {
  font-family: 'DM Mono', monospace;
  font-size: 10px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Edit button — hidden until row hover */
.segment-edit-btn {
  width: 18px;
  height: 18px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: var(--text-muted);
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s, background 0.1s;
}
.segment-item:hover .segment-edit-btn { opacity: 0.6; }
.segment-edit-btn:hover { opacity: 1 !important; background: var(--hover-bg); color: var(--accent); }

/* Delete button — hidden until row hover */
.segment-delete-btn {
  width: 18px;
  height: 18px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s, background 0.1s;
}
.segment-item:hover .segment-delete-btn { opacity: 0.6; }
.segment-delete-btn:hover { opacity: 1 !important; background: rgba(217,107,90,0.1); color: var(--danger); }

/* Inline edit input (replaces span temporarily) */
.segment-inline-input {
  font-family: inherit;
  font-size: 12px;
  padding: 1px 4px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  background: var(--modal-bg);
  color: var(--text);
  outline: none;
  min-width: 40px;
}
.segment-inline-input.title-input {
  flex: 1;
  min-width: 0;
}
.segment-inline-input.duration-input {
  font-family: 'DM Mono', monospace;
  font-size: 10px;
  width: 48px;
  text-align: center;
}

/* Split MM:SS duration editor */
.duration-edit-wrapper {
  display: inline-flex;
  align-items: center;
  gap: 0;
}
.duration-edit-wrapper .duration-sep {
  font-family: 'DM Mono', monospace;
  font-size: 10px;
  color: var(--text-muted);
  padding: 0 1px;
}
.segment-inline-input.duration-mm,
.segment-inline-input.duration-ss {
  font-family: 'DM Mono', monospace;
  font-size: 10px;
  text-align: center;
  padding: 1px 2px;
}
.segment-inline-input.duration-mm {
  width: 26px;
  border-radius: 4px 0 0 4px;
  border-right: none;
}
.segment-inline-input.duration-ss {
  width: 22px;
  border-radius: 0 4px 4px 0;
}

/* Add segment button at bottom of list */
.segment-add-btn {
  width: calc(100% - 16px);
  margin: 4px 8px 6px;
  padding: 5px;
  border: 1px dashed var(--border);
  border-radius: 6px;
  font-size: 11px;
  color: var(--text-muted);
  transition: all 0.15s;
  background: none;
  cursor: pointer;
  flex-shrink: 0;
}
.segment-add-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ═══════════════════════════════════════════════════
   SESSION ACTIONS MENU (kebab dropdown)
   ═══════════════════════════════════════════════════ */
.session-actions-wrapper {
  position: relative;
  flex-shrink: 0;
}

.session-actions-menu {
  position: absolute;
  left: 8px;
  bottom: calc(100% - 18px);
  margin-bottom: 4px;
  background: var(--modal-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  padding: 4px 0;
  min-width: 160px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
  transition: opacity 0.15s, transform 0.15s;
  z-index: 20;
}
.session-actions-menu.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.menu-item {
  display: block;
  width: 100%;
  padding: 7px 14px;
  font-size: 12px;
  text-align: left;
  white-space: nowrap;
  transition: background 0.1s;
  cursor: pointer;
  background: none;
  border: none;
  color: var(--text);
  font-family: inherit;
}
.menu-item:hover { background: var(--hover-bg); }

/* Touch devices: always show edit/delete buttons, hide drag handles */
@media (hover: none) {
  .segment-drag-handle { display: none; }
  .segment-edit-btn { opacity: 0.5; }
  .segment-delete-btn { opacity: 0.5; }
}

/* ═══════════════════════════════════════════════════
   SHORT VIEWPORT
   ═══════════════════════════════════════════════════ */
@media (max-height: 500px) {
  .session-panel-inner { max-height: 120px; }
}
