:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #252542;
  --border: #3a3a5c;
  --text: #f0f0f5;
  --text-muted: #9898b0;
  --accent: #7c6af7;
  --accent-hover: #9585ff;
  --danger: #ff6b8a;
  --radius: 14px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: "Segoe UI", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

body {
  overscroll-behavior: none;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(88px + var(--safe-bottom));
}

.header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(15, 15, 26, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
}

.header h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.tabs {
  display: flex;
  gap: 6px;
  padding: 0 12px 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}

.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.sub-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 0 12px 12px;
}

.sub-tabs.sub-tabs--many {
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  grid-template-columns: unset;
}

.sub-tabs.sub-tabs--many::-webkit-scrollbar {
  display: none;
}

.sub-tabs.sub-tabs--many .sub-tab {
  flex: 0 0 auto;
  min-width: 4.5rem;
}

.sub-tab {
  border: none;
  background: var(--surface2);
  color: var(--text-muted);
  border-radius: 10px;
  padding: 10px;
  font-size: 0.9rem;
  cursor: pointer;
}

.sub-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--border);
}

.main {
  flex: 1;
  padding: 12px 12px 0;
}

.search-bar input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 1rem;
  outline: none;
}

.list-controls {
  display: grid;
  gap: 10px;
}

.sort-bar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sort-bar label {
  flex: 0 0 auto;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.sort-bar select {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  outline: none;
}

.sort-bar select:focus {
  border-color: var(--accent);
}

.entry-dates {
  margin: 0 0 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.entry-dates em {
  font-style: normal;
  color: var(--text);
  opacity: 0.85;
  margin-right: 2px;
}

.date-sep {
  margin: 0 6px;
  opacity: 0.5;
}

.search-bar input:focus {
  border-color: var(--accent);
}

.entry-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.entry-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
}

.entry-card:active {
  transform: scale(0.985);
}

.entry-card:hover {
  border-color: var(--accent);
}

.entry-title {
  margin: 0 0 6px;
  font-size: 1.05rem;
  font-weight: 600;
}

.entry-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}

.chip {
  display: inline-block;
  font-size: 0.75rem;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface2);
  color: var(--text-muted);
}

.chip--filter {
  cursor: pointer;
}

.chip--filter:hover {
  background: var(--accent);
  color: white;
}

.entry-note {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 48px 16px;
}

.fab {
  position: fixed;
  right: max(16px, calc(50% - 320px + 16px));
  bottom: calc(20px + var(--safe-bottom));
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  font-size: 1.8rem;
  line-height: 1;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.fab:active {
  background: var(--accent-hover);
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
}

.icon-btn.csv-btn {
  width: auto;
  padding: 0 10px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.form-dialog {
  width: min(100%, 520px);
  max-height: 90dvh;
  margin: auto;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.form-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.form-dialog form {
  display: flex;
  flex-direction: column;
  max-height: 90dvh;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
  border-bottom: 1px solid var(--border);
}

.dialog-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.form-body {
  overflow-y: auto;
  padding: 12px 16px;
  display: grid;
  gap: 12px;
}

.field label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  outline: none;
}

.field textarea {
  min-height: 88px;
  resize: vertical;
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  border-color: var(--accent);
}

.field--checkbox {
  padding: 4px 0;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 0.95rem;
}

.checkbox-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.field-hint {
  margin: 6px 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.form-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 16px calc(16px + var(--safe-bottom));
  border-top: 1px solid var(--border);
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 0.95rem;
  cursor: pointer;
}

.btn.primary {
  background: var(--accent);
  color: white;
}

.btn.lookup-btn {
  flex: 0 0 auto;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 10px 12px;
  font-size: 0.82rem;
  white-space: nowrap;
}

.btn.lookup-btn:disabled {
  opacity: 0.6;
}

.title-row {
  display: flex;
  gap: 8px;
}

.title-row input {
  flex: 1;
  min-width: 0;
}

.field--title {
  position: relative;
}

.title-suggestions {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  max-height: 220px;
  overflow-y: auto;
  box-shadow: var(--shadow);
  z-index: 5;
}

.title-suggestion-item,
.title-suggestion-msg {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.title-suggestion-item:last-child,
.title-suggestion-msg:last-child {
  border-bottom: none;
}

.title-suggestion-item strong {
  display: block;
  font-size: 0.92rem;
  margin-bottom: 2px;
}

.title-suggestion-item span {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.title-suggestion-item.active,
.title-suggestion-item:hover {
  background: var(--surface2);
}

.title-suggestion-msg {
  cursor: default;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.lookup-query {
  margin: 0;
  padding: 0 16px 8px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.lookup-list {
  list-style: none;
  margin: 0;
  padding: 8px 16px 16px;
  max-height: 50dvh;
  overflow-y: auto;
}

.lookup-item {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  cursor: pointer;
}

.lookup-item:hover {
  border-color: var(--accent);
}

.lookup-item strong {
  display: block;
  margin-bottom: 4px;
}

.lookup-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.lookup-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 24px 16px;
}

.btn.secondary {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
  margin-right: auto;
}

@media (min-width: 641px) {
  .app {
    padding-top: 12px;
  }

  .header {
    border-radius: var(--radius);
    margin: 0 12px;
    border: 1px solid var(--border);
  }
}
